function a(){}
a.prototype.age=1
new a().age//1
var p=Object.create(new a)
p.age//1
a.prototype.age=4
p.age//4
Object.create(a.prototype).age//4
Object.create(a.prototype).__proto__ === a.prototype//true
Object.create(new a).__proto__ === a.prototype//false
Object.create(new a().__proto__).__proto__ === a.prototype//true
new a().__proto__ === a.prototype//true
创建一个具有指定原型且可选择性地包含指定属性的对象
var fun = function(){}
Object.create(fun.prototype,descriptors).__proto__ === fun.prototype
-
prototype
:必需。 要用作原型的对象。 可以为 null -
descriptors
:可选。 包含一个或多个属性描述符的 JavaScript 对象 -
返回值:一个具有指定的内部原型且包含指定的属性(如果有)的新对象。
-
若要停止原型链,可以使用采用了 null prototype 参数的函数。 所创建的对象将没有原型
Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString(), hasOwnProperty()等方法
var a = {}
a.toString()
//"[object Object]"
var b = Object.create(null)
b.toString()
//VM273:1 Uncaught TypeError: b.toString is not a function
descriptors
- 该参数对象不能是 undefined
- 只有该对象中自身拥有的可枚举的属性才有效,也就是说该对象的原型链上属性无效
var newObj = Object.create(null, {
size: {
value: "large",//默认值,默认undefined
enumerable: true,//默认false
writable:true,//是否可修改默认false 不能和get/set共存
configurable:false,//默认false
},
shape: {
enumerable: true,
get(){
return 'shape'
},
set(){
console.log('set')
}
}
});