[英]What's the difference between typeof foo and typeof(foo) / delete bar and delete(bar), and why do we need both?
As far as I can tell, typeof foo and typeof(foo) behave the same, and the latter is much more intuitive because I can just think of it as a function. 据我所知,typeof foo和typeof(foo)的行为相同,后者更直观,因为我可以将其视为一个函数。
The same for delete() . 与delete()相同。
So, is there a difference? 那么,有区别吗? If not, why do we need both forms?
如果没有,为什么我们需要两种形式?
The parentheses are optional. 括号是可选的。
typeof
is an operator, not a function. typeof
是运算符,而不是函数。 That's like asking why do 5 + 3
and not 5 + (3)
. 就像问为什么
5 + 3
而不是5 + (3)
。
In most cases it will not make a difference however, it becomes useful when you are using typeof to evaluate the type of an expression. 在大多数情况下,它没有什么区别,但是当您使用typeof评估表达式的类型时,它会变得很有用。
typeof(1 + ' some text');
returns 退货
string
However 然而
typeof 1 + ' some text';
returns 退货
"number some text"
Both typeof
and delete
are unary operators but with a big difference between the two: typeof
evaluates its operand and delete
does not. typeof
和delete
都是一元运算符,但两者之间有很大区别: typeof
评估其操作数,而delete
则不。 Operator precedence ( MDN reference ) is useful for discussion. 运算符优先级( MDN参考 )对于讨论很有用。
delete
UnaryExpression delete
UnaryExpression
The delete operator does not evaluate its operand and ignores grouping operators '(' and ')' around it because they have higher precedence than delete
. delete运算符不评估其操作数,并忽略其周围的分组运算符'('和')',因为它们的优先级高于
delete
。 Hence delete
could never be written as function call using the same syntax because a function call would evaluate function arguments. 因此
delete
绝不能使用相同的语法编写为函数调用,因为函数调用会评估函数参数。 Delete can use property short cut or lookup syntax: 删除可以使用属性快捷方式或查找语法:
delete objectIdentifier.property name; // is equivalent to
delete objectIdentifier["propertyName"]
In strict mode a syntax error is generated if you try using delete
on anything that is not a reference to an object property that includes both the property name and object. 在严格模式下,如果您尝试在不引用包含属性名称和对象的对象属性的任何对象上使用
delete
,则会生成语法错误。
typeof
UnaryExpession typeof
UnaryExpession
The typeof operator evaluates its operand. typeof运算符评估其操作数。 If the operand expression is enclosed in parentheses, the entire expression between the parentheses is evaluated before applying
typeof
to the result. 如果操作数表达式包含在括号中,则在将
typeof
应用于结果之前,将评估括号之间的整个表达式。 Using parentheses around an operator that can be identified using rules for operator precedence alone is redundant. 在运算符周围使用括号(仅使用运算符优先级的规则即可识别)是多余的。
Summary 摘要
Using parentheses around an operand for delete
doesn't help and is probably best avoided. 在操作数周围使用括号进行
delete
无济于事,最好避免。
Use parentheses around a typeof
operand to obtain the type of an expression result. 在
typeof
操作数周围使用括号可获取表达式结果的类型。
Placing parentheses around typeof
and its operand, as in (typeof operand)
can be useful in debugging a complex expression if operator precedence or automatic type conversions are not working as expected. 配售括号周围
typeof
和它的操作数,如在(typeof operand)
可以在调试复杂表达式,如果操作者的优先级或类型自动转换为预期不工作是有用的。
The use of parentheses around operands for delete
and typeof
does not create two kinds of syntax for the operators - the parentheses follow normal rules for parsing an expression. 在
delete
和typeof
操作数周围使用括号不会为运算符创建两种语法-括号遵循解析表达式的常规规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.