Skip to content

Instantly share code, notes, and snippets.

@vadym1930
Created January 11, 2017 21:43
Show Gist options
  • Save vadym1930/d46cce045a3b6f2fac83c9da86bc0e45 to your computer and use it in GitHub Desktop.
Save vadym1930/d46cce045a3b6f2fac83c9da86bc0e45 to your computer and use it in GitHub Desktop.
OOP prototypal pattern example
var human = {
species: "human",
sayName: function() {
console.log("My name is " + this.name);
},
isAlive: true,
/**
* create an object with set of properties. Getting object within needing
* properties and values and in will be the instanse of current object
*/
create: function(myObject) { // must be an object
var instance = Object.create(this);
var keys = Object.keys(myObject);
keys.forEach(function(key) {
instance[key] = myObject[key];
});
return instance;
}
}
var vampire = Object.create(human);
vampire.name = "Victor";
vampire.isAlive = false;
var gerge = human.create("George");
var hanSolo = human.create({
name: "Solo",
planet: "Dagobag",
sayName: function(){
console.log("My nama is " +"this.name");
},
friends: ["Chupaka", "luk"]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment