javascript Object.create()函数

来源:互联网 发布:淘宝怎么推广和宣传 编辑:程序博客网 时间:2024/05/17 22:15

Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象。
语法

 Object.create(proto, [ propertiesObject ])

参数

proto一个对象,应该是新创建的对象的原型。propertiesObject可选。该参数对象是一组属性与值,该对象的属性名称将是新创建的对象的属性名称,值是属性描述符

测试

1参数proto为原型对象

//实现继承function Father(){}function Child(){}Child.prototype=Object.create(Father.prototype);console.log(Child.prototype.__proto__===Father.prototype)//trueconsole.log(Child.__proto__===Father.__proto__)//true//function A(){}var a=Object.create(A.prototype);//无prototype属性console.log(a.__proto__===A.prototype)//true

2参数proto为函数对象

function A(){}var a1=Object.create(A)//有prototype属性console.log(a1.prototype===A.prototype)//trueconsole.log(a1.__proto__===A)//true//对比var a2=new A();//无prototype属性console.log(a2.__proto__===A.prototype)//true

3参数proto为函数实例

function A(){}var A1=new A();var a=Object.create(A1);//无prototype属性console.log(a.__proto__===A1)//true
原创粉丝点击