帶你手動實現Object.create()方法

1.我自己的實現

 // 我自己的實現 Object.create()
 Object.create = function (proto, properties = {}) {
  	let newObj = {};
    newObj.__proto__ = proto;
    Object.defineProperties(newObj, properties);
    return newObj;
  }

2.其他人的實現方式

function create(proto) {
  function F() {};
   F.prototype = proto; // 將原型掛在構造函數的prototype上
   F.prototype.constructor = F;
   return new F(); // 返回新對象
}

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