这是一个非常基本的问题,只是为了满足我的好奇心,但是有没有办法做这样的事情:
if(obj !instanceof Array) {
//The object is not an instance of Array
} else {
//The object is an instance of Array
}
这里的关键是能够使用 NOT !在实例前面。通常我必须设置的方式是这样的:
if(obj instanceof Array) {
//Do nothing here
} else {
//The object is not an instance of Array
//Perform actions!
}
当我只想知道对象是否是特定类型时,必须创建一个 else 语句有点烦人。
最佳答案
用括号括起来并在外面取反。
if(!(obj instanceof Array)) {
//...
}
在这种情况下,优先顺序很重要。 见:Operator Precedence .
!
运算符在 instanceof
运算符之前。
关于Javascript !instanceof If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8875878/