Created
January 11, 2017 21:43
-
-
Save vadym1930/d46cce045a3b6f2fac83c9da86bc0e45 to your computer and use it in GitHub Desktop.
OOP prototypal pattern example
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
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