javascript isInstanceOf
javascript isInstanceOf
这个问题已经有了答案:
可能的重复:
x = 'myname'; x.intanceOf == String
为什么第二个语句返回false?如何检查一个变量是否为字符串?
admin 更改状态以发布 2023年5月21日
使用instanceOf可能并不是一个好主意。
typeof操作符(连同instanceof)可能是JavaScript中最大的设计缺陷,因为它几乎完全失效了。
请参见:http://bonsaiden.github.com/JavaScript-Garden/#types.typeof
相反,请使用 Object.prototype.toString,像这样:
function is(type, obj) { var clas = Object.prototype.toString.call(obj).slice(8, -1); return obj !== undefined && obj !== null && clas === type; } is('String', 'test'); // true is('String', new String('test')); // true