// specify reference of prototypr of current objectvarperson1={name:"Qiushi"};// is same asvarperson1=Object.create(Object.prototype,{name:{enumerable:true,configurable:true,writable:true,value:"Qiushi"}});
// inherit from other objectsvarperson1={name:"Nick",sayName:function(){console.log("My name is "+this.name)}};// anotherCompany自己定义了name property,它覆盖了从company那里继承来的name property.varperson2=Object.create(person1,{name:{enumerable:true,configurable:true,writable:true,value:"Jeff"}});person1.sayName();// My name is Nickperson2.sayName();// My name is Jeffconsole.log(person1.hasOwnProperty("sayName"));// trueconsole.log(person2.hasOwnProperty("sayName"));// falseconsole.log(person1.isPrototypeOf(person2));// trueconsole.log(Object.getPrototypeOf(person2)==person1);// true
这里值得注意的是,在创建person2时,它的prototype指向了person1对象本身,而不是person1对象的prototype.通常我们在使用var obj = new Book()的时候,obj指向Book函数的prototype,而上面我们显示地指定了让person的prototype指向person1对象自身.
functionYourConstructor(){// initialization}// JavaScript engine does this for you behind the scenesYourConstructor.prototype=Object.create(Object.prototype,{constructor:{configurable:true,enumerable:true,value:YourConstructor,writable:true}});