繁体   English   中英

类型错误:console.log(...) 不是函数

[英]TypeError: console.log(...) is not a function

我真的很困惑如何让 console.log is not a function on line 1091. 如果我删除下面的闭包,第 1091 行不会抱怨这样的错误。 Chrome 版本 43.0.2357.130(64 位)。

在此处输入图片说明

这是代码:

$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);
            }
        }
    }
};

解决方案

只需在console.log( ... )后放一个分号 ( ; )


解释

该错误很容易重现,如下所示:

 console.log() (function(){})

它试图将function(){}作为参数传递给console.log()返回值,它本身不是一个函数,但实际上是undefined (检查typeof console.log(); )。 这是因为 JavaScript 将其解释为console.log()(function(){}) 然而, console.log一个函数。

如果你没有console对象,你会看到

参考错误:控制台未定义

如果您有console对象但没有您会看到的log方法

类型错误:console.log 不是函数

然而,你所拥有的是

类型错误:console.log(...) 不是函数

注意函数名后面的(...) 对于那些,它指的是函数的返回值。

由于 JavaScript 的自动分号插入 (ASI) 规则,换行符不会将这两个表达式作为单独的语句分开。


尊重;

如果没有分号,所有这些代码片段都会导致各种意外错误:

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
let a, b;
const array = Array.from({ length: 2 })

// Now, let’s use destructuring:
[a, b] = array; // ReferenceError: can't access lexical declaration 'array' before initialization
let a, b;
const array = Array.from({ length: 2 }).fill(1),
  array2 = Array.from({ length: 2 })

// Now, let’s use destructuring. Attempt to get the two 1’s from `array` as `a` and `b`:
[a, b] = array;
console.log(a, b); // undefined undefined

另一个例子

您经常会在使用链式方法或链式属性访问器时看到(...)

string.match(/someRegEx/)[0]

如果没有发现,正则表达式,该方法将返回null和属性访问null将导致TypeError: string.match(...) is null -返回值为null console.log(...)的情况下,返回值undefined

2020 更新

一种可能的原因可能是脚本中某处的var console声明。

用:

window.console.log(...);

反而。 为我工作。

我希望它有帮助

该错误意味着console.log()返回值不是函数。 你缺少一个分号:

console.log('xxx', $scope.tableIndexes[i].columnName[j]);
//                                                      ^

这使得 IIFE 的以下(...)被解释为函数调用。


比较错误信息

> var foo = {bar: undefined};
> foo.bar();
Uncaught TypeError: foo.bar is not a function

> var foo = {bar: function(){}};
> foo.bar()();
Uncaught TypeError: foo.bar(...) is not a function

还有另一种方法可以遇到此错误。 console.log不是一成不变的,并且可能会意外覆盖该值。

console.log = 'hi';

在这种情况下,只需重新加载页面即可消除损坏。

我知道这不是“THE”答案,但我想我会在以下内容中折腾

 var console = $( data.message_target );
 console.val( console.val() + data.message); 
 console.scrollTop(console[0].scrollHeight - console.height());

我在我称之为“控制台”的页面上有一个文本区域。 突然我所有的 console.log() 脚本都给出了错误“Uncaught TypeError: console.log is not a function at Object”

...这是正确的,因为我为我的对象/变量使用了一个保留的命名空间。 读完他的帖子后,我意识到我做了什么,为了后代:仔细检查命名约定。

干杯

“它总是人为错误”

至少在 react-native 中,控制台似乎无需任何导入即可工作,因此,删除import console = require('console'); import console from 'console'; 从我的文件开始就为我修复了它。 (VS Code IDE 似乎有时会自动添加)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM