跳到正文

手写练习/面向对象

GitHub

手写 new

中等OOP

代码实现

JavaScript21 行
function myNew(Constructor, ...args){
    const obj = Object.create(Constructor.prototype)
    //立即执行构造函数
    const result = Constructor.apply(obj, args)
    //如果构造函数显式返回对象,则 new 返回这个对象
    return (result !== null &&( typeof result === 'object' || typeof result === 'function')) ? result : obj
}


function Person(name) {
  this.name = name;
}
Person.prototype.say = function () {
  return `hi ${this.name}`;
};

// 1. 基础构造
const p1 = myNew(Person, "Alice");
console.log(p1.name); // Alice
console.log(p1.say()); // hi Alice
console.log(p1 instanceof Person); // true