Advertisement
Guest User

My new NEW

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Person(saying) {
  2.     this.saying = saying;
  3. }
  4.  
  5. Person.prototype.talk = function () {
  6.     console.log(`It's 5 AM so:  ${this.saying}`);
  7. };
  8.  
  9. function myNew(constructor) {
  10.    // 1. create a new object;
  11.    let obj = {};
  12.  
  13.    // 2. set the prototype
  14.    Object.setPrototypeOf(obj, constructor.prototype);
  15.  
  16.    // 3. execute the constructor with this... take the args after 0
  17.    constructor.apply(obj, Array.from(arguments).slice(1));
  18.  
  19.    // 4. return the object
  20.    return obj;
  21. }
  22.  
  23. let me = new Person('bla bla bla');
  24. me.talk();
  25.  
  26. let testPerson = myNew(Person, 'go to gym');
  27. testPerson.talk();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement