rya*_*dlf 170 javascript instanceof
这是一个非常基本的问题,只是为了满足我的好奇心,但有没有办法做这样的事情:
if(obj !instanceof Array) {
//The object is not an instance of Array
} else {
//The object is an instance of Array
}
Run Code Online (Sandbox Code Playgroud)
这里的关键是能够使用NOT!在实例面前.通常我必须设置它的方式是这样的:
if(obj instanceof Array) {
//Do nothing here
} else {
//The object is not an instance of Array
//Perform actions!
}
Run Code Online (Sandbox Code Playgroud)
当我只想知道对象是否是特定类型时,必须创建一个else语句有点烦人.
Ser*_*sev 331
用括号括起来,在外面否定.
if(!(obj instanceof Array)) {
//...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,优先顺序很重要(https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence).的!operator在instanceof运算符之前.
小智 73
if (!(obj instanceof Array)) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
是否正确检查这一点 - 正如其他人已经回答的那样.已经提出的另外两种策略是行不通的,应该理解......
在!没有支架的操作员的情况下.
if (!obj instanceof Array) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,优先顺序很重要(https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence).该!运营商先于instanceof运营商.所以,首先!obj评估false(相当于! Boolean(obj)); 那么你正在测试是否false instanceof Array,这显然是消极的.
在!运营商之前的instanceof运营商的情况下.
if (obj !instanceof Array) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
这是语法错误.运算符如!=单个运算符,而不是应用于EQUALS的运算符.没有这样的操作员,就像!instanceof没有!<操作员一样.
Wil*_*ilt 40
很容易忘记括号(括号),这样你就可以养成以下习惯:
if(obj instanceof Array === false) {
//The object is not an instance of Array
}
Run Code Online (Sandbox Code Playgroud)
要么
if(false === obj instanceof Array) {
//The object is not an instance of Array
}
Run Code Online (Sandbox Code Playgroud)
在这里试试吧
| 归档时间: |
|
| 查看次数: |
56517 次 |
| 最近记录: |