Method of string object in JS and method of acquiring prototype + custom method

Source: Internet
Author: User
 <script type= "Text/javascript" > var str= "ABCDE";//var str=new String ("ABCDE");
        Str.length//String length Str.bold ()//To font bold equivalent to <b>abcde</b> str.fontcolor ("red")/To add color to the font Str.link ("http://www.163.com");///Make the Font hyperlink str.substr (1,3);//A substring of the specified length starting at the specified position str.substring (1,3);

    /method returns a string containing the substring from start to the last (not including end). </script> 
    <script type= "Text/javascript" >
    * * found that the String object method in JS is limited and wants to manipulate other functions of the string.
    * For example: Remove the space at both ends of the string. This can only be customized. ///
    remove spaces at both ends of the string.
    function Trim (str) {
    //define two variables, where a record begins. The position at which a record ends.
    //The beginning of the position of the character to judge, if it is a space, it is incremented until it is not a space.
    //The end of the position of the character to judge, if it is a space, it is decremented until it is not a space.
    //You must ensure that the beginning of the <= is completed so that interception can take place.
    var start,end;
    start=0;
    end=str.length-1;
    while (Start<=end && str.charat (start) = = ") {
        start++;
    }
    while (Start<=end && str.charat (end) = "") {
        end--;
    }
    Return str.substring (start,end+1);
    }
    </script>

Since the Trim method is a way to manipulate strings, can you, like a string,
The method is also defined in a string object. Called directly with a string object.
This can be accomplished by using a prototype property of the string:
Prototype: is a description of the object. If new features are added to the description.
Then the object will have these new features.
And prototype can get to this prototype object.
The function of an object can be extended by prototype.

Requirements: You want to add a new feature to the string object that removes whitespace from both ends of the string.
You can use the prototype property to complete it.

String.prototype.trim = function () {
var start,end;
start=0;
end=this.length-1;
while (Start<=end && this.charat (start) = = ") {
start++;
}
while (Start<=end && this.charat (end) = "") {
end--;
}
Return this.substring (start,end+1);
}


In the call time can be
var  s= "SDASD";
S.trim ();

/Add a feature to the prototype of string. Note: Add new features to an object and use the object directly. New content.
String.prototype.len = 199;//Adds a property name Len to the prototype object for string. Value is 199.

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.