Implicit invocation of valueof, ToString in JavaScript

Source: Internet
Author: User

On the group today, someone asked a question like:

Function add allows for continuous addition operations
The function add syntax is as follows
Add (NUM1) (num2) (num3) ...;//note here is the ellipsis Yo, infinity
Examples of use are:
Add (10) (10) = 20;
Add (10) (20) (50) = 80;
Add (10) (20) (50) (100) = 180;
Please use the JS code to implement the function Add.

                      Add (num) { var  sum=num, TMP  =function   (v) {sum  +=v;                 return   TMP};  tmp.tostring  =function   () { return   sum};  return   TMP} alert (Add ( 10) (20) (()) // 80   

At first I didn't understand why I returned 80, because the ToString method has not been called, how does it return sum, and later know that ToString was implicitly called, and then there is the following.

To quote a passage first:

Each object's ToString and ValueOf methods can be rewritten, each object executed, and if used to manipulate the JavaScript parser will automatically call the object's ToString or valueof method, lifting chestnuts:

    //let's start by creating an object and modifying its ToString and valueof methods    varobj={i:10, ValueOf:function() {Console.log (' performed the valueof () '); return  This. i+20}, ToString:function() {Console.log (' Executed ToString () '); return  This. ValueOf () +20        }    }        //when we call:alert (obj)//50 executed ToString () performed the valueof ()Alert (+obj)//30 performed the valueof ()Alert (OBJ>40)//false executes the valueof ()Alert (obj==30)//true executes the valueof ()Alert (obj===30)//false    //the last one does not output any strings, the personal guess is this: when compared with the same, the JS parser directly first determine whether the type is the same, the obvious one is an object, a number, so directly unequal, there is no need to go to the evaluation. 

As can be seen, although we do not invoke any method of obj, but to use obj to operate, it seems (in fact, if we are the creator of the JS parser is not a problem, eh) JS parser automatically help us call its ToString or valueof method.

The next step is to explore when to execute the ToString method and when to execute the ValueOf method. Roughly guess this: if you do subtraction, comparison operation is the ValueOf method, if you need to specify the value of the implementation is the ToString method. More rigorous experiment copy, please forgive (original address):

See here, we look back at the implicit conversions we used before with strings and numbers plus and minus operations:

    var age =;     // ' year old '    // ' years old '

When you run age.tostring () + ' year old ', the age variable is explicitly converted from number to type string by calling the ToString function. When we run the Age + ' years-old ', we do not have an explicit call function, and we are also converted to the string type, where the JavaScript engine does an implicit type conversion. Implicit type conversion can be thought of as the appropriate type conversion function called by the JavaScript engine for type conversion, and age + ' years-old ' is actually equivalent to age.tostring () + ' years old ', implicit type conversion and explicit type conversion is actually a matter (of course , specifically how excellent to do the implicit conversion or build JS parser is the most clear, he how not to tell us how to build it ... (☆▽☆) ... Is this game fun? ).
Haha, in fact, is not before the implicit type conversion to do in-depth research, only know, oh is so, implicit type conversion. So I don't know what's going on in the form of the Add function above, but now it's clear.

Reference:

Http://www.cnblogs.com/rubylouvre/archive/2010/10/01/1839748.html

Http://zjuwwq.gitbooks.io/jump_javascript/content/data_types/type_conversion.html

Implicit invocation of valueof, ToString in JavaScript

Related Article

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.