Difference between newA. B () and newA (). B () in Javascript

Source: Internet
Author: User
Tags getbase
NewAB () is different from newA () B (). We all know that I have never understood why. This issue involves priority issues between vertex operators, new operators, and function execution. NewAB (); the logic is this new. B () and new (). B () is different. As we all know, I have never understood why. This issue involves priority issues between vertex operators, new operators, and function execution. The logic of new A. B (); is as follows: new A. B (); The dot operator takes precedence over the new operator. It seems that this is only true. New (). B (); the logic is as follows: (new ()). B (); not new ((). b) (); the difference is that A is followed by A pair of parentheses, which affects the priority order. The description of The new Operator (11.2.2) and function cils in The ECMAScript standard is as follows: 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. R Eturn 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 argu Ment 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 CILS The production CallExpression: MemberExpression Arguments is evalu Ated 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, thenIf IsPropertyReference (ref) is true, thenLet thi SValue be GetBase (ref ). else, the base of ref is an Environment RecordLet 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 Call Expression: CallExpression Arguments is evaluated in exactly the same manner, alias t 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. I was a little scared when I looked at such a large logic, but I understood it in half. In Javascript, if the constructor does not contain parameters, parentheses can be omitted when new is used. // The two statements are equivalent var d = new A; var d = new A (); // but the two statements are different and cannot be confused: var d = new. B (); // new. b; var d = new (). B (); // new (). b;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.