Created
March 4, 2013 15:53
-
-
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".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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