TypeError: console.log(…) is not a function

2017-09-03 20:12:31

Simply put a semicolon (;) after console.log().


Explanation

The error is easily reproducible like this:

It’s trying to pass function(){} as an argument to the return value of console.log() which itself is not a function but actually undefined (check typeof console.log();). This is because JavaScript interprets this as console.log()(function(){})console.log however is a function.

If you didn’t have the console object you’d see

ReferenceError: console is not defined

If you had the console object but not the log method you’d see

TypeError: console.log is not a function

What you have, however, is

TypeError: console.log(...) is not a function

Note the (...) after the function name. With those it’s referring to the return value of the function.


Respect the ;

All these code snippets result in all sorts of unexpected errors if no semicolons are present:

console.log() // As covered before() // TypeError: console.log(...) is not a functionconsole.log() // Accessing property 0 of property 1 of the return value…[1][0] // TypeError: console.log(...) is undefinedconsole.log() // Like undefined-3-3 // NaN

Another Example

You see the (...) oftentimes with the use of chained methods or chained property accessors:

string.match(/someRegEx/)[0]

If that RegEx isn’t found, the method will return null and the property accessor on null will cause a TypeError: string.match(...) is null — the return value is null. In the case of console.log(...) the return value was undefined.


  • 2019-01-23 20:56:57

    YouTube视频爬虫-批量采集-低成本解决方案-技术难点和细节回顾

    对于我们这些国内玩家而言,实现youtube视频爬虫和批量采集有先天性的遗憾。起初,公司需要一大批的youtube视频,时长3分钟左右,720p下载的话,每视频30-50M左右。公司雇了一大批人,采购科学上网神器手工下载 ,无奈,效率之低令人发指。所以老板要我做爬虫自动采集,需求每天下载2000+个视频,视频存储需要提高国内访问速度,方便合作方的程序抓取我们的内容。 --------------------- 作者:ucsheep 来源:CSDN 原文:https://blog.csdn.net/ucsheep/article/details/81380342 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-01-24 16:11:39

    数据库去除重复记录

    如何删除数据库中重复的记录 一般情况下,数据库去重复有以下那么三种方法:

  • 2019-01-26 10:12:40

    一行代码让webview不加载图片

    最近项目中需要控制列表页和详情页图片资源的显示,列表页比较好做,详情页是用WebView来展示的,不太好控制图片资源的加载。在Google上找到了两个解决办法,跟大家分享一下!