Research on valueof and tostring Methods

Source: Internet
Author: User

At last, some strange questions were discussed in the group, and some experts wanted to think about the two things. Although they failed, this spirit is very encouraging. So I decided to write an articleArticleTo introduce them.

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

Let's take a look at the following example:

 
// By situ zhengmei 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

<Br/> var AAA = {<br/> I: 10, <br/> valueof: function () {return this. I + 30 ;}, <br/> tostring: function () {return this. valueof () + 10 ;}< br/>}< br/> alert (AAA> 20); // true <br/> alert (+ AAA ); // 40 <br/> alert (AAA); // 50 <br/>

RunCode

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!

 
VaR BBB = {I: 10, tostring: function () {console. log ('string'); return this. i;}, valueof: function () {console. log ('valueof '); return this. I ;}} alert (BBB); // 10 tostring alert (+ BBB); // 10 valueof alert (''+ BBB ); // 10 valueof alert (string (BBB); // 10 tostring alert (number (BBB); // 10 valueof alert (BBB = '10 '); // true valueof alert (BBB = '10'); // false

<Br/> var BBB = {<br/> I: 10, <br/> tostring: function () {<br/> console. log ('string'); <br/> return this. i; <br/>}, <br/> valueof: function () {<br/> console. log ('valueof '); <br/> return this. i; <br/>}</P> <p> alert (BBB); // 10 tostring <br/> alert (+ BBB ); // 10 valueof <br/> alert (''+ BBB); // 10 valueof <br/> alert (string (BBB )); // 10 tostring <br/> alert (number (BBB); // 10 valueof <br/> alert (BBB = '10 '); // true valueof <br/> alert (BBB = '10'); // false <br/>

Run code

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.

 
VaR AA = {I: 10, tostring: function () {console. log ('string'); return this. I ;}} alert (AA); // 10 tostring alert (+ AA); // 10 tostring alert (''+ AA ); // 10 tostring alert (string (AA); // 10 tostring alert (number (AA); // 10 tostring alert (AA = '10 '); // true tostring

<Br/> var AA = {<br/> I: 10, <br/> tostring: function () {<br/> console. log ('string'); <br/> return this. i; <br/>}</P> <p> alert (AA); // 10 tostring <br/> alert (+ AA ); // 10 tostring <br/> alert (''+ AA); // 10 tostring <br/> alert (string (AA )); // 10 tostring <br/> alert (number (AA); // 10 tostring <br/> alert (AA = '10 '); // true tostring <br/>

Run code

Let's look at valueof.

 var BB = {I: 10, valueof: function () {console. log ('valueof '); return this. I ;}} alert (bb); // [object] Alert (+ BB); // 10 valueof alert (''+ BB ); // 10 valueof alert (string (bb); // [object] Alert (number (bb); // 10 valueof alert (BB = '10 '); // true valueof 

<Br/> var BB = {<br/> I: 10, <br/> valueof: function () {<br/> console. log ('valueof '); <br/> return this. i; <br/>}</P> <p> alert (bb); // [object] <br/> alert (+ BB ); // 10 valueof <br/> alert (''+ BB); // 10 valueof <br/> alert (string (bb )); // [object] <br/> alert (number (bb); // 10 valueof <br/> alert (BB = '10 '); // true valueof <br/>

Run code

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.

 object. prototype. tostring = NULL; var cc = {I: 10, valueof: function () {console. log ('valueof '); return this. I ;}} alert (CC); // 10 valueof alert (+ CC); // 10 valueof alert (''+ CC ); // 10 valueof alert (string (CC); // 10 valueof alert (number (CC); // 10 valueof alert (Cc = '10 '); // true valueof 

<Br/> object. prototype. tostring = NULL; </P> <p> var cc = {<br/> I: 10, <br/> valueof: function () {<br/> console. log ('valueof '); <br/> return this. i; <br/>}</P> <p> alert (CC); // 10 valueof <br/> alert (+ CC ); // 10 valueof <br/> alert (''+ CC); // 10 valueof <br/> alert (string (CC )); // 10 valueof <br/> alert (number (CC); // 10 valueof <br/> alert (Cc = '10 '); // true valueof <br/>

Run code

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.