Javascript解惑之 new A.B() 与 new A().B() 的区别
来源:互联网 发布:语音导航软件下载 编辑:程序博客网 时间:2024/04/30 08:52
new A.B() 与 new A().B() 是有区别的,这个大家都知道,至于为什么,我之前一直不是很理解。
这个问题涉及到的知识面是,点运算符、new运算符、函数执行这三者之间的优先级的问题。
点运算符优先于new运算符,看起来似乎仅仅如此。
new A().B(); 的逻辑却是这样的: (new A()) .B();而不是 new (A().B) ();区别在于A后面多了一对小括号,这个影响到了优先级顺序。
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).
- If IsPropertyReference(ref) is true, then
- 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.
NOTEThe 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的时候可以省略括号。
//这两种写法是等价的var d = new A;var d = new A();//但是下面这两种是不同的,不能混淆了:var d = new A.B(); //new A.B;var d = new A().B(); //new A().B;
- Javascript解惑之 new A.B() 与 new A().B() 的区别
- A a=new B()的意义
- A *a=new B();
- A *a=new B();
- A *a=new B();
- A a = new B();
- String a = "abc" 与String b = new String("abc")的区别
- A ab=new B();
- A *p=new B;
- a&&b与a&b的区别
- A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C();
- A b=new B()子类的向上转型
- 关于A a = new B()
- C#中A a=new B()的意义
- C#中A a=new B()的意义
- (a+b)/2与a+(b-a)/2的区别
- a = a + b 与 a += b 的区别
- String a =new String(“abc”);和String b=”abc”;的区别
- C面试题--之^运算。
- 嵌入式bootloader开发之八----NAND Flash读写擦除操作实现(Tiny 6410)
- 基于IAP的STM32程序更新技术
- 搞服务器学Centos
- 小公司工作一月小感
- Javascript解惑之 new A.B() 与 new A().B() 的区别
- 数据结构 概述
- (阶段三 dijkstra温习1.3)HDU 1874 畅通工程续(用dijkstra求单源起点和单点终点的最短路径(起点和终点动态给出))
- 博弈论入门
- JAVA设计模式(1)
- POJ 2152
- request.getSession(true)和request.getSession(false)的区别
- Linux之printf的缓冲区问题
- PHP 文件获取到数组 yahoo 音乐