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