/ Published in: JavaScript
# addMethod - By John Resig (MIT Licensed)
Add a method to an object. Creates function overloading scenario based entirely on length of argument list. Won't work to overload functions according to argument "type".
# inherit
Create parameter-less inheritance chain for situations in which the parent object takes arguments in its "constructor".
Usage:
function A(a, b) { }
function B() { A.call(this, 1, 2); }
B.prototype = inherit(A.prototype);
Add a method to an object. Creates function overloading scenario based entirely on length of argument list. Won't work to overload functions according to argument "type".
# inherit
Create parameter-less inheritance chain for situations in which the parent object takes arguments in its "constructor".
Usage:
function A(a, b) { }
function B() { A.call(this, 1, 2); }
B.prototype = inherit(A.prototype);
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/*jslint plusplus: true, vars: true, browser: true, devel: false, maxerr: 5, maxlen: 140 */ function ObjectBoilerPlateModule() { "use strict"; // addMethod - By John Resig (MIT Licensed) // add a method to an object. // creates function overloading scenario based entirely on length of argument list. // won't work to overload functions according to argument "type". function addMethod(object, name, fn) { var old = object[name]; object[name] = function () { if (fn.length === arguments.length) { return fn.apply(this, arguments); } if (typeof old === 'function') { return old.apply(this, arguments); } throw "Invalid"; }; } function inherit(parentPrototype) { function F() {} F.prototype = parentPrototype; return new F(); } this.addMethod = addMethod; this.inherit = inherit; }