Last active
December 10, 2015 20:08
-
-
Save skeep/4485640 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 cat = function(obj){ | |
//this is where I copy all the attributes of the passed object to the constructure | |
for (attr in obj){ | |
this[attr] = obj[attr]; | |
} | |
//publice method | |
this.talk = function(){ | |
console.log('meeow!!!'); | |
}; | |
}; | |
//extend the class | |
cat.prototype.run = function(){ | |
console.log('running!!!'); | |
}; | |
//this can be locally defined, receive from user input or fetch from localstorage | |
var obj = { | |
name : 'sona', | |
age : 24, | |
fav : { | |
color : ['red'], | |
number : [1, 2, 3] | |
} | |
}; | |
// recreate the object | |
var cat1 = new cat(obj); | |
cat1.talk(); | |
cat1.run(); | |
console.log(cat1); | |
var cat2 = new cat(obj); | |
//extend the class again | |
cat.prototype.walk = function(){ | |
console.log('walking'); | |
}; | |
//the new method is automatically avaiable to the new object although originally they were not defined. | |
cat2.walk(); | |
cat1.walk(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment