Skip to content

Instantly share code, notes, and snippets.

@gotomanners
Created September 12, 2012 22:08
Show Gist options
  • Save gotomanners/3710304 to your computer and use it in GitHub Desktop.
Save gotomanners/3710304 to your computer and use it in GitHub Desktop.
Javascript inheritance(prototype)
Object.make = function make (proto) {
var o = Object.create(proto);
var args = [].slice.call(arguments, 1);
args.forEach(function (obj) {
Object.getOwnPropertyNames(obj).forEach(function (key) {
o[key] = obj[key];
});
});
return o;
}
Base = function(param){
this.init(param);
}
Base.prototype = {
init: function(param){
this.param = param;
},
calc: function(){
var result = this.param * 10;
document.write("Result from calc in Base: " + result + "<br/>");
},
calcB: function(){
var result = this.param * 20;
document.write("Result from calcB in Base: " + result+ "<br/>");
}
}
Extend = function(param){
this.init(param);
}
Extend.prototype = Object.make(Base.prototype, {
constructor: Extend,
calc: function(){
var result = this.param * 50;
document.write("Result from calc in Extend: " + result+ "<br/>");
}
});
extendedObj = new Extend(10);
extendedObj.calc();
extendedObj.calcB();​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment