关键词
new与属性访问的顺序 优先级 结合性 执行顺序 求值顺序
描述:
如果不是,那么和 new Class() 有什么区别吗?
来源:《基于 MVC 的 JavaScript 富应用》P7
参考来源
ECMAScript 标准里关于 new 运算符 (11.2.2) 和 function calls 的描述是下面这一段:
11.2.2 The new Operator
The production NewExpression : new NewExpression is evaluated as follows:
Let ref be the result of evaluating NewExpression.
Let constructor be GetValue(ref).
If Type(constructor) is not Object, throw a TypeError exception.
If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
Return the result of calling the [[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).
The production MemberExpression : new MemberExpression Arguments is evaluated as follows:
Let ref be the result of evaluating MemberExpression.
Let constructor be GetValue(ref).
Let argList be the result of evaluating Arguments, producing an internal list of argument values (11.2.4).
If Type(constructor) is not Object, throw a TypeError exception.
If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
Return the result of calling the [[Construct]] internal method on constructor, providing the list argList as the argument values.
11.2.3 Function Calls
The production CallExpression : MemberExpression Arguments is evaluated as follows:
Let ref be the result of evaluating MemberExpression.
Let func be GetValue(ref).
Let argList be the result of evaluating Arguments, producing an internal list of argument values (see 11.2.4).
If Type(func) is not Object, throw a TypeError exception.
If IsCallable(func) is false, throw a TypeError exception.
If Type(ref) is Reference, then
If IsPropertyReference(ref) is true, then
Let thisValue be GetBase(ref).
Else, the base of ref is an Environment Record
Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).
Else, Type(ref) is not Reference.
Let thisValue be undefined.
Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values.
The production CallExpression : CallExpression Arguments is evaluated in exactly the same manner, except that the contained CallExpression is evaluated in step 1.
NOTE The returned result will never be of type Reference if func is a native ECMAScript object. Whether calling a host object can return a value of type Reference is implementation-dependent. If a value of type Reference is returned, it must be a non-strict Property Reference.
结论
在 JavaScript 中,如果构造函数不带参数的话,new 的时候可以省略括号。
所以以下两种写法等价
|
|
深入
但是下面这两种就是不同的
|
|
这里其实就考察了一个操作符的优先级(注意:指的是优先结合)和结合性:具体可以参考我的总结JavaScript运算符
当一个操作数同时位于一个操作符的右边和一个操作符的左边,应该如何添加括号 => 先看优先级
如果优先级相等的情况下,再看结合性,是左结合还是右结合以决定添加如何添加括号
|
|
以上你可以学到什么?
特殊的语法
new与属性访问的顺序