1. 創建一個新對象
2. 將這個新對象的隱式原型指向Class的顯式原型
3. 將這個新對象作爲this指針的指向,用call/apply調用Constructor
4. 如果上述Constructor調用返回對象或function就將其指向 賦值號(=) 左側變量,否則將這個新對象指向左側變量
// Demo
const man = new Person();
等同於:
const obj = {};
Reflect.setPrototypeOf(obj, Person.prototype)(瀏覽器內可使用obj.__proto__ = Person.prototype)
const res = Person.apply(obj, arguments);
if(typeof(res) === "object" || typeof(res) === "function"){
return res;
} else {
return obj;
}
歡迎關注、點贊