Object.create (proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不是一个子函数,可以传一个null,第二个参数是对象的属性描述符,这个参数是可选的。
function Car (desc) {
this.desc = desc;
this.color = "red";
}
Car.prototype = {
getInfo: function() {
return 'A ' + this.color + ' ' + this.desc + '.';
}
};
//instantiate object using the constructor function
var car = Object.create(Car.prototype);
car.color = "blue";
alert(car.getInfo());
结果为:A blue undefined.
2.propertiesObject 参数的详细解释:(默认都为false)
数据属性
writable:是否可任意写
configurable:是否能够删除,是否能够被修改
enumerable:是否能用 for in 枚举
value:值
访问属性:
get(): 访问
set(): 设置
详细说明参考MDN 中的object.create( )介绍
具体三个步骤就是:
创建一个对象
继承指定父对象
为新对象扩展新属性