JS extract integer part, remove the first last space

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.