TypeError: invalid 'instanceof' operand 'x'
メッセージ
TypeError: invalid 'instanceof' operand "x" (Firefox) TypeError: "x" is not a function (Firefox) TypeError: Right-hand side of 'instanceof' is not an object (Chrome) TypeError: Right-hand side of 'instanceof' is not callable (Chrome)
エラータイプ
何がうまくいかなかったのか?
instanceof
演算子 は、右側のオペランドがコンストラクターオブジェクトであることを想定しています。つまり、右側のオペランドは prototype
プロパティを持ち、呼び出し可能であるオブジェクトである必要があります。
例
"test" instanceof ""; // TypeError: invalid 'instanceof' operand ""
42 instanceof 0; // TypeError: invalid 'instanceof' operand 0
function Foo() {}
var f = Foo(); // Foo() is called and returns undefined
var x = new Foo();
x instanceof f; // TypeError: invalid 'instanceof' operand f
x instanceof x; // TypeError: x is not a function
これらのエラータイプを修正するには、instanceof
演算子 を typeof
演算子 に置き換えるか、評価結果の代わりに関数名を使用するようにしてください。
typeof "test" == "string"; // true
typeof 42 == "number" // true
function Foo() {}
var f = Foo; // Do not call Foo.
var x = new Foo();
x instanceof f; // true
x instanceof Foo; // true