TypeError:console.log(...)不是函数 [英] TypeError: console.log(...) is not a function

查看:170
本文介绍了TypeError:console.log(...)不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑,我怎么能得到console.log不是第1091行的功能。如果我删除下面的闭包,第1091行不会抱怨这样的错误。 Chrome版本43.0.2357.130(64位)。

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).

以下是代码:

$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( ... )之后加一个分号(; ) )

Solution

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

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

The error is easily reproducible like this:

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

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

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.

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

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


ReferenceError:控制台未定义

ReferenceError: console is not defined

如果您有控制台对象但不是 log 你看到的方法

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


TypeError:console.log不是函数

TypeError: console.log is not a function

但你拥有的是


TypeError:console.log(...)不是函数

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.

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

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]

如果找不到该RegEx,该方法将返回 null null 上的属性访问器将导致 TypeError:string.mat ch(...)为null - 返回值 null 。在 console.log(...)的情况下,返回值 undefined

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.

这篇关于TypeError:console.log(...)不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆