最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the correct JavaScript operator precedence table? - Stack Overflow

programmeradmin5浏览0评论

If I run the following code on Firefox, I get an error:

new Number.toString;

But according to MDN, new Number should evaluate first. So the table is not correct, I think.

Let us take a look at MSDN. Above the table is written that operators are evaluated from left to right. But:

a = 1;
b = a = 2;

Now b has the value 2 which suggest evaluation from right to left. So this precedence table is also not correct.

What is the correct table?

If I run the following code on Firefox, I get an error:

new Number.toString;

But according to MDN, new Number should evaluate first. So the table is not correct, I think.

Let us take a look at MSDN. Above the table is written that operators are evaluated from left to right. But:

a = 1;
b = a = 2;

Now b has the value 2 which suggest evaluation from right to left. So this precedence table is also not correct.

What is the correct table?

Share Improve this question edited Sep 14, 2022 at 13:03 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 16, 2014 at 10:17 Marco de WitMarco de Wit 2,8042 gold badges19 silver badges23 bronze badges 3
  • Associativity, it matters. – raina77ow Commented Jan 16, 2014 at 10:20
  • 3 Regarding why new Number.toString() is incorrect (not just Gecko) stackoverflow./questions/21100001/… – marekful Commented Jan 16, 2014 at 10:21
  • 2 I am looking for a precedence table which explains that. – Marco de Wit Commented Jan 16, 2014 at 10:31
Add a ment  | 

3 Answers 3

Reset to default 6

according to https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence$revision/510297#Table new Number should evaluate first. So the table is not correct I think.

The new Operator is plicated. Let's check the official language grammar: It does occur in two manifestations:

MemberExpression := new MemberExpression Arguments | …
NewExpression := new NewExpression | …

The latter, where is called without arguments, does indeed have a lesser precedence than the property accessors - so that your expression evaluates as new (Number.toString). However, when new is called with arguments (parenthesis), then it does have a greater precedence than a CallExpression and is equal to a property accessor, in which case they'd evaluate left-to-right. Indeed, the MDN table should make this more clear.

Let us take a look at MSDN: http://msdn.microsoft./en-us/library/z3ks45k7(v=vs.94).aspx . Above the table is written that operators are evaluated from left to right.

This is definitely wrong. Operator associativity is not always left-to-right, most obvious at the assignment operators as in your example. The MDN table states this correct. Also, MSDN seems to oversimplify the precedence of postfix operators.

Can anyone give me a correct table?

Try my new revision of MDN's table.

Here is MDN's full operator precedence table, available at your link:

Javascript precedence are more impotant, some learning website does not give proper list, we should careful about this.

发布评论

评论列表(0)

  1. 暂无评论