Create function () { // create a null object the let obj = new new Object () // constructor obtain the let Con = [] .shift.call (arguments) // set point new object constructor __proto__ property prototype property obj .__ = Con.prototype proto__ // this binding, this refers to the function a new instance of an object. the Result = Con.apply the let (obj, arguments The) // make sure it is a new target return typeof the Result === 'Object' the Result:? obj }
// concise version of the new implementation process
1 2 3 4 5 6 7 8 9 10
newfunc function (constructor) { // Step 1: Create an empty object obj var obj = {}; // Step: prototype object constructor prototype constructor assigns obj obj .__ proto__ = constructor.prototype; / / the third step: in this constructor constructor point obj, and to perform internal operations constructor immediately constructor.apply (obj); // step Four: returns this object return obj; }