There’s a whole mess of ways to accomplish this (see this article). Obviously, YMMV, but I chose this route for one simple reason: it makes sense to me. Anyhow, here’s a method of creating an extensible object in Javascript.
var theObject = function (config) { this.exProperty = ""; this.exFunction = function () { console.log("exFunction base function."); } this.constructor = function (c) { // You can put init code before this line. for(p in c) { this[p] = c[p]; } }; this.constructor(config); }; /*** * EXAMPLE */ var test = new theObject({ // Instantiate object exProperty: "Setting exProperty", exFunction: function () { return "exFunction overridden."; }, exFunction2: function () { console.log("Extending theObject."); } }); console.log(test.exProperty); // "Setting exProperty" console.log(test.exFunction()); // "exFunction overridden."
I could have went the prototype route, but I’m quite the lazy typer ;-). Feedback is welcome!