Categories
JavaScript

JavaScript Augmentation of types

Say we want to add methods to a certain type. For example, adding a trim function to the String type:

String.method(‘trim’, function (  ) {
    return this.replace(/^s+|s+$/g, ”);
});

document.writeln(‘”‘ + ”   neat   “.trim(  ) + ‘”‘);

Leave a Reply

Your email address will not be published. Required fields are marked *