Created
April 15, 2016 15:02
-
-
Save Chunlin-Li/834e7322e55dd79286f280ab241663a9 to your computer and use it in GitHub Desktop.
javascript nodejs Object create proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(){ | |
this.name = 'anonymous' | |
} | |
Person.prototype.toJSON = function toJSON(){ | |
return 'Person ' + this.name; | |
}; | |
Person.prototype.pro01 = 'yes'; | |
var p1 = new Person(); | |
p1.name = 'the first person'; | |
p1.obj01 = 'yes'; | |
console.log(p1); | |
console.log(p1.constructor === Person); | |
console.log(p1.constructor.prototype === p1.__proto__); | |
console.log(p1.constructor.prototype === Person.prototype); | |
console.log(p1.toJSON === Person.prototype.toJSON); | |
console.log('-----------------'); | |
var p2 = Object.create(Person.prototype); | |
p2.name = 'the second person'; | |
console.log(p2.obj1 === undefined); | |
console.log(p2.toJSON === Person.prototype.toJSON); | |
console.log(p2.constructor === Person); | |
console.log(p2.constructor.prototype === Person.prototype); | |
console.log('-------------------'); | |
var p3 = Object.create(p1); | |
console.log(p3); | |
console.log(p3.name === p1.name); | |
console.log(p3.obj01 === p1.obj01); | |
console.log(p3.constructor === Person); | |
console.log(p3.constructor.prototype === Person.prototype); | |
console.log(p3.__proto__ === p1); // ****** | |
console.log(Object.prototype === p3.__proto__); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment