Skip to content

Instantly share code, notes, and snippets.

@ohvitorino
Created May 6, 2016 14:27
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 ohvitorino/fc83aeb99f15fcb84dcc035b97888c23 to your computer and use it in GitHub Desktop.
Save ohvitorino/fc83aeb99f15fcb84dcc035b97888c23 to your computer and use it in GitHub Desktop.
Prototypal inheritance in javascript (new stuff)
//prototypal inheritance
var human = {
species: "human",
create: function(values) {
var instance = Object.create(this);
Object.keys(values).forEach(function(key) {
instance[key] = values[key];
});
return instance;
},
saySpecies: function() {
console.log(this.species);
},
sayName: function() {
console.log(this.name);
}
};
var musician = human.create({
species: "musician",
playInstrument: function() {
console.log("plays " + this.instrument);
}
});
var will = musician.create({
name: "Will",
instrument: "drums"
});
will.playInstrument(); // plays drums
will.sayName(); //will
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment