Skip to content

Instantly share code, notes, and snippets.

@NKid
Last active December 11, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NKid/8040ecc477cd21954dc1 to your computer and use it in GitHub Desktop.
Save NKid/8040ecc477cd21954dc1 to your computer and use it in GitHub Desktop.
Object.create()
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