Skip to content

Instantly share code, notes, and snippets.

@skeep
Last active December 10, 2015 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeep/4485640 to your computer and use it in GitHub Desktop.
Save skeep/4485640 to your computer and use it in GitHub Desktop.
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