[英]How can I detect whether it is safe to use `in` operator in Javascript?
This statement 这个说法
if ('foo' in bar)
throws a TypeError if bar
is a string. 如果
bar
是字符串,则抛出TypeError。 I presume it's possible to have the same error for other types of object. 我认为其他类型的对象可能会出现相同的错误。
How can I tell if bar
is an object which supports the in
operator? 如何判断
bar
是否是支持in
运算符的对象?
This code isn't sufficient: if foo
is a string, it passes through to the in
statement and throws. 这段代码是不够的:如果
foo
是一个字符串,它将传递给in
语句并抛出。
if (foo instanceof Object && ! (foo instanceof String))
{
if ('foo' in foo) {}
}
The in
operator throws a TypeError is the right operand is not an object. in
运算符将引发TypeError是正确的操作数不是对象。
You can check if a value is an object using 您可以使用以下方法检查值是否为对象
Object(value) === value;
Additionally, in
operator calls internal [[HasProperty]] method. 另外,
in
操作员中调用内部的[[HasProperty]]方法。 For ordinary objects it should never throw , but it might be the case in some exotic objects. 对于普通对象,永远不要抛出 ,但是在某些奇异对象中可能就是这种情况。 For example,
例如,
var proxy = new Proxy({}, {has: () => {throw "Can't use `in`"}});
"property" in proxy; // Error: Can't use `in`
To handle these cases, I think you can only use a try
statement, as ringø suggested . 为了处理这些情况,我认为您只能使用
try
语句,如ringø建议 。
var o = {};
console.log(o.constructor === Object && o !== null); //true
To catch any error 捕捉任何错误
try {
if ('foo' in bar) {
}
}
catch(e) {
// 'foo' is not in bar
}
or if you want just to catch errors for 'foo' in bar
或者,如果您只想捕获
'foo' in bar
错误
var inbar = false;
try { if ('foo' in bar) inbar = true; }
catch(e) {}
if (inbar) {
...
}
How can I detect whether it is safe to use
in
operator in Javascript?如何检测Javascript
in
运算符是否安全?
The production RelationalExpression : RelationalExpression in ShiftExpression is evaluated as follows:
ShiftExpression中的生产RelationalExpression:RelationalExpression的评估如下:
- Let lref be the result of evaluating RelationalExpression.
令lref为评估RelationalExpression的结果。
- Let lval be GetValue(lref).
令lval为GetValue(lref)。
- Let rref be the result of evaluating ShiftExpression.
令rref为评估ShiftExpression的结果。
- Let rval be GetValue(rref).
令rval为GetValue(rref)。
- If Type(rval) is not Object, throw a TypeError exception.
如果Type(rval)不是Object,则抛出TypeError异常。
- Return the result of calling the [[HasProperty]] internal method of rval with argument ToString(lval).
返回使用参数ToString(lval)调用rval的[[HasProperty]]内部方法的结果。
Emphasis mine. 强调我的。
In ES5 Type()
can be 在ES5中,
Type()
可以是
So to prevent TypeError
s from happening, you have to make sure that it's not 因此,为防止
TypeError
发生,您必须确保它不会
undefined
null
true
or false
true
还是false
EcmaScript 2015 introduces the Symbol
type as well. EcmaScript 2015也引入了
Symbol
类型。
An example function might look like: 一个示例函数可能类似于:
function canCallIn(arg) {
if (arg === null ||
arg === undefined)
return false;
switch (typeof arg) {
case 'boolean':
case 'string':
case 'symbol':
case 'number':
return false;
}
return true;
}
Although you may be able to make various optimizations. 尽管您可以进行各种优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.