Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Created February 12, 2017 21:22
Show Gist options
  • Save m-x-k/0da2b9b8b1c02205124a229a826f5efc to your computer and use it in GitHub Desktop.
Save m-x-k/0da2b9b8b1c02205124a229a826f5efc to your computer and use it in GitHub Desktop.
Javascript augment String prototype to include trim function
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
String.method('trim', function ( ) {
return this.replace(/^\s+|\s+$/g, '');
});
document.writeln('"' + " neat ".trim( ) + '"');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment