TypeError: console.log(...) は関数ではありません [重複] 質問する

TypeError: console.log(...) は関数ではありません [重複] 質問する

1091 行目の console.log is not a function をどうやって取得するのか、本当に混乱しています。以下のクロージャを削除すると、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);
            }
        }
    }
};

ベストアンサー1

解決

…の後にセミコロン(;)を置くだけです。console.log()


説明

このエラーは次のように簡単に再現できます。

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

それはfunction(){}議論として通そうとしている戻り値自体console.log()は関数ではありませんが、実際はundefined( を確認typeof console.log();) です。これは、JavaScript がこれを と解釈するためですconsole.log()(function(){})console.logしかし機能。

もしあなたがそのconsole物体を持っていなかったら

ReferenceError: コンソールが定義されていません

オブジェクトはあるconsoleがメソッドがないlog場合は、

TypeError: console.log は関数ではありません

しかし、あなたが持っているのは

TypeError: console.log(...) は関数ではありません

関数名の後の に注意してください(...)。これは関数の戻り値を参照します。

改行これら2つの表現を区別しない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戻り値ですnullconsole.log(...)戻り値だったundefined

おすすめ記事