This article describes how to simulate the process code of a jsnew object. This article describes how to simulate the process code of a js new object.
Function Person () {this. name = 1234; // return {};} Person. prototype. getname = function () {console. log (this. name);} function CreateObj () {var fn = []. shift. call (arguments); var args = arguments; var obj = new Object (); var ret = fn. apply (obj, args); obj. _ proto __= fn. prototype; return typeof ret === 'object '? Ret: obj;} var a = CreateObj (Person); console. log ();
The above code CreateObj simulates the process of an object in js new. From the code of this function, we can clearly see how an object is generated. From here, we can also find that, if the constructor returns an object, the new object is the object returned by the constructor. If the retrun data of other types is ignored by the new one.
The above is a detailed description of the Process Code for simulating a js new object. For more information, see other related articles in the first PHP community!