简体   繁体   English

typeof foo和typeof(foo)/ delete bar和delete(bar)有什么区别,为什么我们都需要?

[英]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. typeofdelete都是一元运算符,但两者之间有很大区别: 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. deletetypeof操作数周围使用括号不会为运算符创建两种语法-括号遵循解析表达式的常规规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 typeof foo ['bar']!=='undefined'与foo中的'bar' - typeof foo['bar'] !== 'undefined' vs. 'bar' in foo let foo = ({bar() {}}) 和 let foo = {bar() {}} 有什么区别? - What is the difference between let foo = ({bar() {}}) and let foo = {bar() {}}? Boolean(foo.bar)和!! foo.bar有什么区别? - What is the difference between Boolean(foo.bar) and !!foo.bar? 为什么foo和bar都是平行的? - Why foo and bar both are parallel? js中“foo.bar”和“foo ['bar']”之间的区别 - Difference between “foo.bar” and “foo['bar']” in js (typeof variable ==='boolean')和(typeof variable =='boolean')之间有什么区别? - What's the difference between (typeof variable === 'boolean') and (typeof variable == 'boolean')? JavaScript中的“ typeof str”和“ typeof(str)”有什么区别? - What's the difference between “typeof str” and “typeof(str)” in JavaScript? 在 React 组件中, foo(){} 和 bar = () => {} 之间有什么区别,我什么时候应该使用哪个? - In a React Component, what's the difference between foo(){} and bar = () => {} and when should I use which? 以下脚本中的 'foo' 和 'bar' 是什么,是否需要定义它们? - What are 'foo' and 'bar' in the following script and do they need to be defined? typeof === 和 typeof == 的区别 - Difference between typeof === and typeof ==
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM