Posts Tagged ‘Augment Types’

//JS allows basic types of the lang 2 b augmented with new functionality & this can be done on Object, Function, String, Number, Regular expressions & booleans

// Ex: Augment Function.prototype with method that makes it available for any function, which can save us typing prototype name while using
Function.prototype.method = function (name, func) {
if(!this.prototype[name])
this.prototype[name] = func;
//return this;
};

// Augment integer method to Number.prototype
Number.method(‘integer’, function () { return Math[this < 0 ? ‘ceil’ : ‘floor’](this); });

document.writeln((-10 / 3).integer() + “<br/>”);

// Augment trim method to String.prototype
String.method(‘trim’, function () { return this.replace(/^\s+|\s+$/g, ”); });
document.writeln(‘”‘ + ”    neat   “.trim() + ‘”‘);

Advertisements