The difference between valueof and ToString in JavaScript

Source: Internet
Author: User

Objective

Basically, all JS data types have these two methods, except for NULL. They both solve the problem of JavaScript value operations and display, and overrides increase the optimizations they call.

Test analysis

Let's look at one example:
var AAA = {
I:10,
Valueof:function () {return this.i+30;},
Tostring:function () {return this.valueof () +10;}
}

Alert (AAA > 20); True
alert (+AAA); 40
Alert (AAA); 50

This results because they secretly invoke the valueof or ToString method.
But how to differentiate which method is called, we can test it by another method.
In the Console.log, please experiment with the FF with Firebug!

var bbb = {
I:10,
Tostring:function () {
Console.log (' toString ');
return this.i;
},
Valueof:function () {
Console.log (' valueOf ');
return this.i;
}
}

Alert (BBB);//ToString
alert (+BBB); Ten ValueOf
Alert (' +bbb '); Ten ValueOf
Alert (String (BBB)); Ten toString
Alert (number (BBB)); Ten ValueOf
Alert (bbb = = ' 10 '); True ValueOf
Alert (bbb = = = ' 10 '); False
The result gives the impression that if the ToString method is called when converting to a string, the ValueOf method is called if it is converted to a numeric value, but two of them are very discordant. One is alert (' +bbb '), the string should be called the ToString method ... Another we can understand for the time being that the = = = operator does not make implicit conversions, so they are not called. To pursue the truth, we need more rigorous experimentation.

var AA = {
I:10,
Tostring:function () {
Console.log (' toString ');
return this.i;
}
}
alert (AA);//ToString
alert (+AA); Ten toString
Alert (' +aa '); Ten toString
Alert (String (AA)); Ten toString
Alert (number (AA)); Ten toString
Alert (aa = = ' 10 '); True toString

Look at valueof again.
var bb = {
I:10,
Valueof:function () {
Console.log (' valueOf ');
return this.i;
}
}

Alert (BB);//[Object Object]
alert (+BB); Ten ValueOf
Alert (' +bb '); Ten ValueOf
Alert (String (BB)); [Object Object]
Alert (number (BB)); Ten ValueOf
Alert (BB = = ' 10 '); True ValueOf

Found a little different?! It is not as uniform as the above ToString.
For that [object object], I would expect to inherit from object, and we'll take it out again.

Object.prototype.toString = null;
var cc = {
I:10,
Valueof:function () {
Console.log (' valueOf ');
return this.i;
}
}


Alert (cc);//ValueOf
alert (+CC); Ten ValueOf
Alert (' +cc '); Ten ValueOf
Alert (String (cc)); Ten ValueOf
Alert (number (cc)); Ten ValueOf
alert (cc = = ' 10 '); True ValueOf

Summary: valueof is biased towards arithmetic, and ToString is biased towards display.
1, when the object is converted (for example: Alert (a)), the ToString method will be called first, if not overridden ToString will call the ValueOf method, if both methods are not rewritten, but the ToString output by object.
2. The ToString method will be called first when strongly converting to a string type, and valueof is preferred when strong to a number.
3, in the case of operators, the valueof priority is higher 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.