Java脚本!instanceof If语句 [英] Javascript !instanceof If Statement

查看:59
本文介绍了Java脚本!instanceof If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的问题,仅是为了满足我的好奇心,但是有没有办法做这样的事情:

This is a really basic question really just to satisfy my curiosity, but is there a way to do something like this:

if(obj !instanceof Array) {
    //The object is not an instance of Array
} else {
    //The object is an instance of Array
}

这里的键可以使用NOT!在实例前面.通常我必须设置的方式是这样的:

The key here being able to use the NOT ! in front of instance. Usually the way I have to set this up is like this:

if(obj instanceof Array) {
    //Do nothing here
} else {
    //The object is not an instance of Array
    //Perform actions!
}

当我只是想知道对象是否是特定类型时,不得不创建else语句有点烦人.

And its a little annoying to have to create an else statement when I simply want to know if the object is a specific type.

推荐答案

用括号括起来并在外面取反.

Enclose in parentheses and negate on the outside.

if(!(obj instanceof Array)) {
    //...
}

在这种情况下,优先顺序很重要( https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence ).这 !运算符先于instanceof运算符.

In this case, the order of precedence is important (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence). The ! operator precedes the instanceof operator.

这篇关于Java脚本!instanceof If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆