
我们在前端开发过程中,会遇到typeerror: invalid 'instanceof' operand 'x' 这样的报错。我们查看console的信息,如下:
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)
分析后,发现:
哪里出错了?
例子
"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





网友评论文明上网理性发言 已有0人参与
发表评论: