js new 對象幹了啥

1、創建一個空對象,並且 this 變量引用該對象,// lat target = {};

2、繼承了函數的原型。// target.proto = func.prototype;

3、屬性和方法被加入到 this 引用的對象中。並執行了該函數func// func.call(target);

4、新創建的對象由 this 所引用,並且最後隱式的返回 this 。// 如果func.call(target)返回的res是個對象或者function 就返回它

function new(func) {

lat target = {};
target.__proto__ = func.prototype;
let res = func.call(target);
if (typeof(res) == "object" || typeof(res) == "function") {
    return res;
}
return target;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章