Adding a method to Object.prototype makes the method available to all objects in such a way that the function, array, string, number, regular expression, and Boolean value are equally applicable. For example, add a method for Function.prototype to make the method available to all functions.
The method is added in order to not enter the prototype attribute. Method is available for all functions
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this;}
1. Use method to add an integer function to the number type to get the numeric integral part.
Number.method (' Integer ', function () { return math[this < 0? ' Ceil ': ' floor ' [this];});
Test: -10/3 = 3.333333 ....
Console.log (( -10/3). Integer ());
>-3//Results
2. Remove the space at the beginning of the string, which is also an oversight of native JS.
String.method (' Trim ', function () { return this.replace (/^\s+|\s+$/g, ');}); Console.log (" trim ". Trim ()); The " trim " String is the one in the trim method.
> "Trim"//Result no spaces
JS extract integer part, remove the first last space