Tostring (), a method that is automatically called

Source: Internet
Author: User
ArticleDirectory
    • Topic extension:

The function of the tostring method is needless to say. This built-in JavaScript method also has a feature: When executing some special methods, such as alert or innerhtml, it will be automatically called by the script parser. This feature obviously helps you to be lazy, and of course also helps to implement some specific functions. To illustrate this feature, we will start with a real development case.

In your web project, you need to output a list HTML like the following in many places:


< Ul >
< Li > Javascript event bubbling application instance </ Li >
< Li > Execute ajax to return the Javascript script in the HTML snippet </ Li >
</ Ul >

Obviously, this is a li structure. Maybe you want to output the HTML of this structure every time. Do you have to spell these Li tags yourself? Can I enter only the text content in the middle of Li every time I call a method, and finally get a complete ul HTML structure simply. Of course,ProgramThere are n methods for implementation. Let's look at the following method. This method is obviously simple and scientific:


Function Ulbuilder ()
{
VaR Lis =   '' ;
This . Addli =   Function (Litext)
{
Lis + =   ' <Li> '   + Litext +   ' </LI> ' ;
};

This . Tostring =   Function ()
{
Return   ' <Ul> '   + Lis +   ' </Ul> ' ;
};
}

VaRUlhtml= NewUlbuilder ();
Ulhtml. addli ('Javascript event bubbling application instance');
Ulhtml. addli ('Execute ajax to return the Javascript script in the HTML snippet');
Alert (ulhtml );

AboveCodeWe have defined a class called ulbuilder, which has two public methods: addli and tostring. The addli method is to add a li tag containing the content, the tostring method generates the ul html that is ultimately required. In the actual use of this class, you do not see the call of the tostring method, but alert (ulhtml) shows the complete ul structure of HTML.

In fact, when alert () is used, the script parser automatically checks the alert variable or the tostring method of the object and calls it. In the above example, we have rewritten the tostring method, which happens to be automatically called and executed by the program.

Topic extension:

1. In some object-oriented development languages, such as C # and Java, there are constructor. This constructor is obviously a member method of the class. To execute the code in it, we do not need to manually call this method. The code defined in this method will be automatically executed during class instantiation. Of course, in order for the system to automatically execute some specific methods, the program architecture usually specifies the naming rules for these methods. For example, the name of the constructor should be the same as the class name. In the preceding example, if you change this. tostring to this. Toul, you can only call alert (ulhtml. Toul () like this to obtain the correct result ()).

2. Because the tostring method can be rewritten, it is sometimes particularly useful. Let's take a look at the example below and try to see how different the output results are after not rewriting and rewriting. This example is obviously not robust. For example, the nested array is not processed, and it is of little practical significance, just to let you know that this can be done.


Array. Prototype. tostring =   Function ()
{
Return   ' [\ '' + This. Join ( ' \ ' ,\ '' ) + ' \ ' ] ' ;
};
VaR Companys = [ ' Adobe ' , ' Apple ' , ' Google ' , ' Intel ' , ' Microsoft ' , ' Oracle ' , ' IBM ' , ' Sun ' ];
Alert (companys );

Here is another example. This example is more practical than the above example. Normally, when alert (JSON) is used, you cannot see the real JSON structure. Only [object] is displayed. Try the following implementation. Maybe this is the result you want. Of course, this method is not perfect. To use this function, please download the relevant JS Library at the official JSON site:


VaR Userinfo =  
{
" Name " : " Mike " ,
" Age " : 23 ,
" Phone " : " 020-87654321 " ,
" Tostring " : Function ()
{
VaR Objstr =   '' ;
For ( VaR Key In   This )
{
If ( Typeof ( This [Key]) =   ' String ' )
Objstr + =   ' " '   + Key +   ' ":" '   +   This [Key] +   ' ", ' ;
}
Return   ' { '   + Objstr. Replace ( / , $ / , '' ) +   ' } ' ;
}
}
Alert (userinfo );

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.