SOURCE

function Dog(name, age) {
    this.name = name;
    this.age = age;
}

Dog.prototype.bark = function () {
    console.log(`${this.name}: wang wang wang!`);
}

Dog.prototype.details = function () {
    console.log(`It's a ${this.age} year-old dog named ${this.name}`);
}

let dog1 = new Dog('yooyoo', 5);
dog1.bark();
dog1.details();
console.log(dog1 instanceof Dog);

/**
 * new 的原理
 * 1.创建一个新对象
 * 2.将原型复制到该对象
 * 3.执行构造函数中的代码(作用域赋给新对象)
 * 4.返回该对象
 */
function myNew() {
    const newObject = new Object();
    const Constructor = [].shift.call(arguments)
    newObject.__proto__ = Constructor.prototype;
    Constructor.apply(newObject, arguments);
    return newObject;
}

let dog2 = myNew(Dog, 'foofoo', 2);
dog2.bark();
dog2.details();
console.log(dog2 instanceof Dog);
console 命令行工具 X clear

                    
>
console