Skip to content

Instantly share code, notes, and snippets.

@AugustoPedraza
Created March 4, 2013 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AugustoPedraza/5083204 to your computer and use it in GitHub Desktop.
Save AugustoPedraza/5083204 to your computer and use it in GitHub Desktop.
Simple augmenting function to "Function.prototype" to extract just the integer part of a number and removes spaces from the ends of a string. This example was taken from the book "Javascript: The Good Parts".
//Define method to add a method available to all functions.
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
Number.method('integer', function( ){
return Math[this < 0 ? 'ceil' : 'floor'](this);
})
console.log(10.3.integer( )); //Show 10
//Define method to add a method available to all functions.
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
String.method('trim', function( ){
var regex = /^\s+|\s+$/g;
return this.replace(regex, '');
});
var dirtyName = " Augusto Pedraza ";
console.log(dirtyName);
console.log(dirtyName.trim( ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment