js之create()

时间:2025-05-01 12:37:44

语法:

Object.create(proto, [propertiesObject])

返回一个新的对象的指针

proto:对象会被作为新创建的对象的原型

[propertiesObject]:对象,自定义的一些自己的属性;

实例1:

var a = {
name1:'jim',
sex1:'nan',
age1:'23'
} var o = Object.create(a,{
name:{
value:'zhangsan',
writable:false
},
age:{
value:null,
writable:true
},
fn:{
configurable:false,
get:function(){
console.log(this.name);
},
set:function(newValue){
this.age = newValue;
console.log(this.name+' : '+this.age);
}
}
});
console.log(o);
o.fn;
o.fn = 23;
console.log(o.name1);
console.log(o.__proto__ === a);//true
省略了的属性特性默认为false