-
-
Save NKid/8040ecc477cd21954dc1 to your computer and use it in GitHub Desktop.
Object.create()
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(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
Person.prototype.sayHi = function() { | |
console.log('Hi') | |
}; | |
var P1 = Object.create(Person.prototype, { | |
name: { | |
value: 'P1', | |
writable: true, | |
enumerable: true | |
}, | |
age: { | |
value: 20 | |
} | |
}); | |
console.log(P1.name); //P1 | |
console.log(P1.age); //20 | |
P1.name = 'abc'; | |
P1.age = '25'; | |
console.log(P1.name); //abc | |
console.log(P1.age); //20 (can't setting) | |
var pKeys = Object.keys(P1); | |
console.log(pKeys); //[ 'name' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment