Javascript 类型错误:console.log(...) 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31013221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: console.log(...) is not a function
提问by Qian Chen
I'm really confused how I can get console.log is not a function on line 1091. If I remove the closure below, line 1091 doesn't complain such error. Chrome Version 43.0.2357.130 (64-bit).
我真的很困惑如何让 console.log is not a function on line 1091. 如果我删除下面的闭包,第 1091 行不会抱怨这样的错误。Chrome 版本 43.0.2357.130(64 位)。
Here is the code:
这是代码:
$scope.columnNameChanged = function (tableColumn) {
setDirtyColumn(tableColumn);
//propagate changes to the key fields
for (var i = 0; i < $scope.tableIndexes.length; ++i) {
for (var j = 0; j < $scope.tableIndexes[i].columnName.length; ++j) {
if ($scope.tableIndexes[i].columnName[j] === tableColumn.previousName) {
console.log('xxx', $scope.tableIndexes[i].columnName[j])
(function (i, j) {
$timeout(function () {
console.log($scope.tableIndexes[i].columnName[j])
$scope.tableIndexes[i].columnName[j] = tableColumn.name.toUpperCase();
console.log($scope.tableIndexes[i].columnName[j])
});
})(i, j);
}
}
}
};
回答by user4642212
Solution
解决方案
Simply put a semicolon (;
) after console.log(
…)
.
只需;
在console.log(
...后面加一个分号 ( ) )
。
Explanation
解释
The error is easily reproducible like this:
该错误很容易重现,如下所示:
console.log()
(function(){})
It's trying to pass function(){}
as an argument to the return valueof 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 isa function.
它试图function(){}
作为参数传递给返回值,console.log()
它本身不是一个函数,而是实际上undefined
(检查typeof console.log();
)。这是因为 JavaScript 将 this 解释为console.log()(function(){})
. console.log
然而是一个函数。
If you didn't have the console
object you'd see
如果你没有console
你会看到的对象
ReferenceError: console is not defined
参考错误:控制台未定义
If you had the console
object but not the log
method you'd see
如果你有console
对象但没有log
你会看到的方法
TypeError: console.log is not a function
类型错误:console.log 不是函数
What you have, however, is
然而,你所拥有的是
TypeError: console.log(...) is not a function
类型错误:console.log(...) 不是函数
Note the (...)
after the function name. With those it's referring to the return value of the function.
注意(...)
函数名后面的。对于那些,它指的是函数的返回值。
The line break doesn't separate these two expressionsas separate statements because of JavaScript's rules for automatic semicolon insertion (ASI).
由于 JavaScript 的自动分号插入 (ASI) 规则,换行符不会将这两个表达式作为单独的语句分开。
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 function
console.log() // Accessing property 0 of property 1 of the return value…
[1][0] // TypeError: console.log(...) is undefined
console.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 valueis null
. In the case of console.log(...)
the return valuewas undefined
.
如果未找到该 RegEx,该方法将返回null
并且属性访问器null
将导致TypeError: string.match(...) is null
?—?返回值是null
。在该情况下console.log(...)
的返回值是undefined
。
回答by Learner
2020 Update
2020 更新
One possible cause can be the declaration of var console
somewhere in your script.
一个可能的原因可能是var console
脚本中某处的声明。
Use:
用:
window.console.log(...);
instead. Worked for me.
反而。为我工作。
I hope it helps
我希望它有帮助
回答by Felix Kling
The error means that the return valueof console.log()
is not a function. You are missing a semicolon:
该错误意味着返回值的console.log()
是不是一个函数。你缺少一个分号:
console.log('xxx', $scope.tableIndexes[i].columnName[j]);
// ^
which makes the following (...)
of the IIFE to be interpreted as a function call.
这使得(...)
IIFE的以下内容被解释为函数调用。
Compare the error messages of
比较错误信息
> var foo = {bar: undefined};
> foo.bar();
Uncaught TypeError: foo.bar is not a function
and
和
> var foo = {bar: function(){}};
> foo.bar()();
Uncaught TypeError: foo.bar(...) is not a function
回答by Craig Pemberton
There is another way to encounter this error. console.log
is not immutable and it is possible to accidentally overwrite the value.
还有另一种方法可以遇到此错误。console.log
不是一成不变的,并且可能会意外覆盖该值。
console.log = 'hi';
In this case just reload the page to undo the damage.
在这种情况下,只需重新加载页面即可消除损坏。
回答by Christian ?agarskas
I know this is not "THE" answer, but thought I would toss in the following
我知道这不是“THE”答案,但我想我会在以下内容中折腾
var console = $( data.message_target );
console.val( console.val() + data.message);
console.scrollTop(console[0].scrollHeight - console.height());
I had a textarea on the page I was calling "console". suddenly all my console.log() scripts gave the error "Uncaught TypeError: console.log is not a function at Object"
我在我称之为“控制台”的页面上有一个文本区域。突然我所有的 console.log() 脚本都给出了错误“Uncaught TypeError: console.log is not a function at Object”
... and rightly so, because I used a reserved namespace for my object/var. I realized what I had done after reading his post, and for the sake of posterity: double check naming conventions.
...这是正确的,因为我为我的对象/变量使用了一个保留的命名空间。读完他的帖子后,我意识到我做了什么,为了后代:仔细检查命名约定。
cheers
干杯
"its always human error"
“它总是人为错误”
回答by Top-Master
In react-native at least, the console seems to work without any import, so, removing import console = require('console');
or import console from 'console';
from the begin of my file fixed it for me. (the VS Code IDE seems to add that automatically sometimes )
至少在 react-native 中,控制台似乎无需任何导入即可工作,因此,删除import console = require('console');
或import console from 'console';
从我的文件开头为我修复了它。(VS Code IDE 似乎有时会自动添加)