Creating a class with a parasitic constructor

Source: Internet
Author: User

Similar to the factory pattern creation class, the difference is that the invocation takes the form of the new constructor.

Role of NEW: Change this point, normal function call, this point to window or the object that called the function, and secondly, after new, the function returns a This object, that is, the instance object

Parasitic constructor: Instead of using the default this return value, return an object with a different, completely separate object from the object this is pointing to.

functionClassA (name) {varobj =NewObject (); Obj.name=name; Obj.printname=function() {console.log (obj.name); Console.log ( This. Name); } console.log ( This, This==obj);//This point ClassA    returnObj//overwrites the returned this, where return will default to the instance of ClassA if it is not used}varo =NewClassA (' Licui '); Console.log (' O instanceof ClassA: ', OinstanceofClassA); Falseo.printname ();

Creating a class with a parasitic constructor

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.