Implicit call of valueOf and toString in JavaScript
Someone asked this question in the group today:
Function add can implement continuous addition operations
The function add syntax is as follows:
Add (num1) (num2) (num3)...; // note that the ellipsis (...) is infinite.
Example:
Add (10) (10) = 20;
Add (10) (20) (50) = 80;
Add (10) (20) (50) (100) = 180;
Use js Code to implement function add.
After thinking about it for a while, you can only Google it. The Code is as follows:
- function add(num){
- var sum=num,
-
- tmp=function(v){
- sum+=v;
- return tmp
- };
-
- tmp.toString=function(){
- return sum
- };
-
- return tmp
- }
-
-
- alert( add(10)(20)(50) ) //80
I didn't understand why the toString method returned 80 at first, because the toString method has never been called. How can I return sum? Later I learned that the toString method was called implicitly, so I can see the following.
Here is a reference:
The toString and valueOf methods of each object can be rewritten. After each object is executed, if it is used to operate the JavaScript parser, The toString or valueOf method of the object will be automatically called. For example:
- // Create an object and modify its toString and valueOf methods.
- Var obj = {
- I: 10,
- ValueOf: function (){
- Console. log ('executed valueOf ()');
- Return this. I + 20
- },
- ToString: function (){
- Console. log ('executed toString ()');
- Return this. valueOf () + 20
- }
- }
- // When we call:
- Alert (obj) // 50 executes toString () and valueOf ()
- Alert (+ obj) // 30 executes valueOf ()
- Alert (obj> 40) // false: valueOf () is executed ()
- Alert (obj = 30) // true executes valueOf ()
- Alert (obj = 30) // false
- // At last, no string is output. I guess this is the case. During full comparison, the js parser directly checks whether the types are the same. Obviously, one is Object and the other is Number, therefore, they do not need to be evaluated.
It can be seen from the above that although we have not called any method of obj, it seems that when we want to use obj for operations, if we are the person who creates the js parser, this is not a problem, AH) The js parser automatically helps us call its toString or valueOf method.
The next step is to explore when the toString method is executed and when the valueOf method is executed. In general, if the valueOf method is executed during addition, subtraction, multiplication, division, and comparison, and if the specific value is required, the toString method is executed. For more rigorous lab copying, please forgive the original article address ):
Here, we will review the previous usageAddition and subtraction of strings and numbersImplicit conversions of other operations:
- var age = 30;
- age.toString() + 'year old'; // '30 year old'
- age + ' years old'; // '30 years old'
When age. toString () + 'year old' is run, the age variable is converted from Number to String by calling the toString function. When we run age + 'years old', we did not explicitly call the function, and age was converted to the String type. Here, the javascript engine has implicitly converted the type. Implicit type conversion can be considered as a type conversion function called by the javascript engine. age + 'ears old' is actually equivalent to age. toString () + 'years old', implicit type conversion and explicit type conversion are actually the same thing, of course, specifically, how can we do implicit conversions? The person who made the js parser knows the most clearly. Why didn't he tell us how to create it... (☆☆ )... is such a game fun ).
Haha, actually, I didn't do any in-depth research on implicit type conversion. I only know, oh, yes, it's 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 ).
Refer:
Http://www.cnblogs.com/rubylouvre/archive/2010/10/01/1839748.html
Http://zjuwwq.gitbooks.io/jump_javascript/content/data_types/type_conversion.html