new 运算符 的四个步骤
- 创建一个空的简单JavaScript对象(即{});
- 链接该对象(即设置该对象的构造函数)到另一个对象 ;
- 将步骤1新创建的对象作为this的上下文 ;
- 如果该函数没有返回对象,则返回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; }
<a name="ZpMiE"></a>
# 如何实现Object.create
```javascript
function create(o){
function F(){};
F.prototype = o;
return new F();
}
备注
实际上 Object.create
有第二个参数
var newObj = Object.create(o, {
b:{
enumerable: false,
writable: true,
configurable: false,
value: 3
},
c:{
enumerable: true,
writable: false,
configurable: false,
value: 4
}
}