The difference between valueof and ToString in JavaScript

Source: Internet
Author: User

Questions:

There are two methods in the JS data type Object.prototype.valueOf and Object.prototype.toString, what is the difference between the instance and the two?

We perform the following example Steps:

Case 1.

    var test={age                        : $,                        toString:function() {return this . age-10;},   //override toString ()                        valueOf:function() {returnthis. age+40;}    Rewrite valueOf ()                  }    

Run the following code:

            Alert (test>20); // true call is ValueOf            // 60 is called ValueOf.            //   The call is ToString

Conclusion: This result occurs because test automatically calls the ValueOf or toString method.

At this point, a new question arises: under what circumstances is the call to valueOf, and what is the invocation of toString?

Keep looking at the following case:

Case 2.

var test={age                        : $,                        tostring:function() {console.log (' ToString ');  returnthis. age-10;},   // override toString ()                        valueOf:function() { Console.log (' valueOf '); return this. age+40;}    // rewrite valueOf ()                  }    

Run the following code:

 Alert (test); //  toString  alert (+test); //  alert (' + Test '); //  alert (String (test)); //  toString  alert (number (test)); //  alert (test = = ' 10 '); //  alert (test = = = ' 10 '); //  

Conclusion: ToString is called when converting to a string, and valueOf is called when converted to a number, but there are two exceptions: an alert (' + test '), which calls ToString instead of valueOf, and another alert (test = = = ' 10 '), the = = = operator does not make an implicit conversion, so they are not called.

To clarify the two exceptions, we continue our case:

Case 3:

var test = {                ,                functionreturnthis. age-10;}   // override toString ()            }

Run the following code:

        Alert (test); // Ten toString            // Ten toString            // Ten toString            // Ten toString            // Ten toString            // true toString            // false

conclusion: when you rewrite ToString only, ToString is Called.

Case 4:

var test = {                ,                functionreturnthis. age + +;}    // rewrite valueOf ()            }

Run the following code:

Alert (test); // [object object]  --inherited by Object.prototype.toString            // valueOf            // valueOf            // [object object]--object.prototype.tostring            // valueOf            // false ValueOf            // false

Conclusion: for that [object object], I would expect to inherit from object, and we'll take it out again.

Case 5:

Object.prototype.toString =NULL; varTest ={age:20,ValueOf:function() {console.log (' valueOf ');return  this. Age + 40; }//rewrite valueOf ()} alert (test);//valueOfAlert (+test);//valueOfAlert (' + Test ');//valueOfAlert (String (test));//valueOfAlert (number (test));//valueOfAlert (test = = ' 10 ');//false ValueOfAlert (test = = = ' 10 ');//false

Case 6:

 var  test = {age:  20 alert (test);  //  [object object]  alert (+test); //  NaN  Alert (' + test); //  [object object]  alert (String (test)); //  [object object]  alert (number (test)); //  NaN  alert (test = = ' 10 '); //  alert (test = = ' 10 '); //  

Case 7:

Object.prototype.toString =NULL; varTest ={age:20} alert (test);//uncaught typeerror:cannot Convert object to primitive valueAlert (+test);//uncaught typeerror:cannot Convert object to primitive valueAlert (' + Test ');//uncaught typeerror:cannot Convert object to primitive valueAlert (String (test));//uncaught typeerror:cannot Convert object to primitive valueAlert (number (test));//uncaught typeerror:cannot Convert object to primitive valueAlert (test = = ' 10 ');//uncaught typeerror:cannot Convert object to primitive valueAlert (test = = = ' 10 ');//falseuncaught typeerror:cannot Convert object to primitive value

From all the above cases, summarize:

1.valueOf is applied to the operation, and ToString is applied to the DISPLAY.

2. When the object is converted to a string (for example, alert (test)), ToString is called first, and if no ToString method is used, the valueof method is called, and if ToString and valueof are not rewritten, follow the The ToString method output of the Object.

3. When a strong-to-string type is used, the ToString method is called first, and valueof is called first when strong to a number.

4. In the case of an operator, the valueof has a higher precedence than tostring.

The difference between valueof and ToString in JavaScript

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.