gpt4 book ai didi

javascript - TypeError : console. log(...) 不是一个函数

转载 作者:行者123 更新时间:2023-11-28 03:47:01 26 4
gpt4 key购买 nike

我真的很困惑如何在第 1091 行获取 console.log is not a function。如果我删除下面的闭包,第 1091 行不会提示此类错误。 Chrome 版本 43.0.2357.130(64 位)。

enter image description here

这是代码:

$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()返回值,而它本身并不是一个函数,但实际上未定义(检查typeof console.log();)。这是因为 JavaScript 将其解释为 console.log()(function(){})console.log 但是一个函数。

如果您没有看到的console对象

ReferenceError: console is not defined

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

TypeError: console.log is not a function

然而,你拥有的是

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

注意函数名称后面的(...)。这些指的是函数的返回值。

换行符doesn’t separate these two expressions由于 JavaScript 的 rules for automatic semicolon insertion (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]

如果未找到该 RegEx,该方法将返回 null 并且 null 上的属性访问器将导致 TypeError: string.match(.. .) 为 null —返回值null。对于 console.log(...)返回值未定义

关于javascript - TypeError : console. log(...) 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48368601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com