Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Person(saying) {
- this.saying = saying;
- }
- Person.prototype.talk = function () {
- console.log(`It's 5 AM so: ${this.saying}`);
- };
- function myNew(constructor) {
- // 1. create a new object;
- let obj = {};
- // 2. set the prototype
- Object.setPrototypeOf(obj, constructor.prototype);
- // 3. execute the constructor with this... take the args after 0
- constructor.apply(obj, Array.from(arguments).slice(1));
- // 4. return the object
- return obj;
- }
- let me = new Person('bla bla bla');
- me.talk();
- let testPerson = myNew(Person, 'go to gym');
- testPerson.talk();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement