Evaluate the valueof and tostring Methods

Source: Internet
Author: User

Basically, all JS data types have the valueof and tostring methods, except null. They solve the Javascript value calculation and display problems.

Valueof () method of JavaScript

The valueof () method returns the original value of a Boolean object.

Use booleanobject. valueof (). The return value is the original Boolean value of booleanobject. If the object that calls this method is not Boolean, an exception typeerror is thrown.

1 <script type="text/javascript">
2 var boo = new Boolean(false);
3 document.write(boo.valueOf());
4 </script>

The above script outputs false.

Tostring () method of JavaScript

The tostring () method converts a logical value to a string and returns the result.

Use booleanobject. tostring () to return a string "true" or "false" based on the original Boolean value or the value of the booleanobject ". If the object that calls this method is not Boolean, an exception typeerror is thrown.

This method is automatically called when a Boolean object is used in a string environment.

The following script creates a Boolean object and converts it to a string:

1 <script type="text/javascript">
2 var boo = new Boolean(true);
3 document.write(boo.toString());
4 </script>

Script output: True.

Let's take a look at the following example:

1 var aaa = {
2     i: 10,
3     valueOf: function() { return this.i+30; },
4     toString: function() { return this.valueOf()+10; }
5 }
6  
7 alert(aaa > 20); // true
8 alert(+aaa); // 40
9 alert(aaa); // 50

This is because they secretly call the valueof or tostring method. But how can we tell which method is called? We can test it using another method. Because console. log is used, please experiment in FF with firebug installed!

01 var bbb = {
02   i: 10,
03   toString: function() {
04     console.log('toString');
05     return this.i;
06   },
07   valueOf: function() {
08     console.log('valueOf');
09     return this.i;
10   }
11 }
12   
13 alert(bbb);// 10 toString
14 alert(+bbb); // 10 valueOf
15 alert(''+bbb); // 10 valueOf
16 alert(String(bbb)); // 10 toString
17 alert(Number(bbb)); // 10  valueOf
18 alert(bbb == '10'); // true valueOf
19 alert(bbb === '10'); // false

At first glance, it seems that the tostring method is called when it is converted to a string, and the valueof method is called when it is converted to a value, but two of them are not harmonious. One is alert (''+ BBB), and the string combination should be to call the tostring method ...... Another operator we can understand for the time being is that the = operator does not perform implicit conversion, so they are not called. We need more rigorous experiments to investigate the truth.

01 var aa = {
02   i: 10,
03   toString: function() {
04     console.log('toString');
05     return this.i;
06   }
07 }
08   
09 alert(aa);// 10 toString
10 alert(+aa); // 10 toString
11 alert(''+aa); // 10 toString
12 alert(String(aa)); // 10 toString
13 alert(Number(aa)); // 10 toString
14 alert(aa == '10'); // true toString

Let's look at valueof.

01 var bb = {
02    i: 10,
03    valueOf: function() {
04      console.log('valueOf');
05      return this.i;
06    }
07 }
08   
09  alert(bb);// [object Object]
10  alert(+bb); // 10 valueOf
11  alert(''+bb); // 10 valueOf
12  alert(String(bb)); // [object Object]
13  alert(Number(bb)); // 10 valueOf
14  alert(bb == '10'); // true valueOf

Is it a little different ?! It is not as unified as the tostring above. For the [object], I guess it is inherited from the object. Let's take a look at it.

01 Object.prototype.toString = null;
02   
03 var cc = {
04   i: 10,
05   valueOf: function() {
06     console.log('valueOf');
07     return this.i;
08   }
09 }
10   
11 alert(cc);// 10 valueOf
12 alert(+cc); // 10 valueOf
13 alert(''+cc); // 10 valueOf
14 alert(String(cc)); // 10 valueOf
15 alert(Number(cc)); // 10 valueOf
16 alert(cc == '10'); // true valueOf

If tostring is overwritten, the existence of valueof is ignored during object conversion. However, if you only override the valueof method, the valueof method is preferred when you want to convert it to a string. When tostring cannot be called, the valueof can only be mounted. For that strange String concatenation problem, it may be out of the operator, open the ECMA262-5 and find that there is a getvalue operation. Well, the answer should be uncovered. Rewriting will increase the optimization of their calls, while in the case of operators, valueof has a higher priority than tostring.

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.