What happens when you NEW a constructor?

  1. Create a new object {}
  2. Point the _proto_ of the new object to the prototpye of the constructor, and the new object can access the prototype object through _proto_
  3. Point this to the newly created function object
  4. Return this if the constructor does not return or if a return value of this is a primitive type; If a reference type is returned, the reference type is returned.

Write a NEW

function myNew(fnc, ... args){
    let obj = new Object(a)// create a new object {}
    obj._proto_=fnc.prototype  // point the _proto_ of the new object to the prototpye of the constructor
    letres = fnc.call(obj,... args)// Point this to the newly created function object
    return res instanceof Object? res:obj }Copy the code