Created
September 12, 2012 22:08
-
-
Save gotomanners/3710304 to your computer and use it in GitHub Desktop.
Javascript inheritance(prototype)
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
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