new 运算符 的四个步骤

  1. 创建一个空的简单JavaScript对象(即{});
  2. 链接该对象(即设置该对象的构造函数)到另一个对象 ;
  3. 将步骤1新创建的对象作为this的上下文 ;
  4. 如果该函数没有返回对象,则返回this 。(不返回基本类型) ```javascript function _new(o){ var newObj = {}; newObj.prototype = o.prototype; newObj.apply(this, arguments.slice(1)); return newObj instanceOf Object ? newObj : this; }

function create(o){ var newObj = Object.create(o.prototype); newObj.apply(this, arguments.slice(1)); return newObj instanceOf Object ? newObj : this; }

  1. <a name="ZpMiE"></a>
  2. # 如何实现Object.create
  3. ```javascript
  4. function create(o){
  5. function F(){};
  6. F.prototype = o;
  7. return new F();
  8. }

备注

实际上 Object.create 有第二个参数

  1. var newObj = Object.create(o, {
  2. b:{
  3. enumerable: false,
  4. writable: true,
  5. configurable: false,
  6. value: 3
  7. },
  8. c:{
  9. enumerable: true,
  10. writable: false,
  11. configurable: false,
  12. value: 4
  13. }
  14. }