Skip to content

Instantly share code, notes, and snippets.

@sokrato
Created October 18, 2013 11:45
Show Gist options
  • Save sokrato/7040400 to your computer and use it in GitHub Desktop.
Save sokrato/7040400 to your computer and use it in GitHub Desktop.
JavaScript Inheritance
var Base = {
create: function(attrs){
var o=this.clone();
o.init(attrs);
return o;
},
clone: function() {
var o={};
for(var i in this)
if (this.hasOwnProperty(i))
o[i] = this[i];
o.constructor.prototype=this;
return o;
},
init: function(attrs) {
for(var i in attrs)
if (attrs.hasOwnProperty(i))
this[i] = attrs[i];
},
extend: function(attrs) {
var o = this.clone();
for(var i in attrs)
if (attrs.hasOwnProperty(i))
o[i] = attrs[i];
return o;
}
}, Person = Base.extend({name:'', age: 0});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment