Object implement
A short routine which safely adds functionality to JavaScript objects. A new Object method called implement is defined which can be used to add new methods to all other objects. The method adds, but does not replace, methods. Added methods are explicitly not enumerable and will therefore not perturb for..in loops.
by Hans PUFAL
JavaScript
/* implement : generic, safe object extension system
Version : 1.01 2012-10-23
Author : Hans B PUFAL : [email protected]
Licence : (c) 2012 under http://www.opensource.org/licenses/mit-license.html
Repository : http://jsfiddle.net/jstoolsmith/nyeeB
Compression : 533 chars with Packer 3.1 @ http://compressorrater.thruhere.net
//----------------------------------------------------------------------------------------
// http://jsfiddle.net/jstoolsmith/nyeeB (c) 2012-10-23 Hans B PUFAL, MIT license
Object.defineProperty(Object.prototype,'implement',{value:function(a,c,d){var e=0;if(Array.isArray(a))for(var b=a.length;b--;)e+=Object.implement.call(this,a[b],null,d);else if(typeof a==='object'){for(b in a)if(a.hasOwnProperty(b)&&typeof a[b]==='function')e+=Object.implement.call(this,b,a[b],d)}else{if(typeof a==='function')d=c,c=a,a=(c.toString().match(/^function\s+([a-z$_][\w$]+)/i)||[0,''])[1];a&&!this.prototype[a]&&typeof c==='function'&&(e=!!Object.defineProperty(this.prototype,a,{configurable:!!d,value:c}))}return e}});
//----------------------------------------------------------------------------------------
*/
Object.defineProperty (Object.prototype, 'implement', {value:
function (mthd, fnc, cfg) { // adds fnc to prototype under name mthd
var r = 0;
if (Array.isArray (mthd)) // array of mthds
for (var m = mthd.length; m--;)
r += Object.implement.call (this, mthd[m], null, cfg);
else if (typeof mthd === 'object') { // object of methods/functions
for (m in mthd) if (mthd.hasOwnProperty (m) && typeof mthd[m] === 'function')
r += Object.implement.call (this, m, mthd[m], cfg);
} else {
if (typeof mthd === 'function') // find mthd name from function source
cfg = fnc, fnc = mthd, mthd =
(fnc.toString ().match (/^function\s+([a-z$_][\w$]+)/i) || [0, ''])[1];
mthd && !this.prototype[mthd] && typeof fnc === 'function' &&
(r =...