Fully parse the valueOf and toString methods in JavaScript (recommended), and fully parse the valueof

Source: Internet
Author: User

Fully parse the valueOf and toString methods in JavaScript (recommended), and fully parse the valueof

In this case, all JS data types have the valueOf and toString methods, except null. They solve the javascript value calculation and display problems. It is widely used in applications. Next we will introduce them one by one.

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.

<script type="text/javascript">var boo = new Boolean(false);document.write(boo.valueOf());</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:

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

Script output: true.

Let's take a look at the following example:

var aaa = {i: 10,valueOf: function() { return this.i+30; },toString: function() { return this.valueOf()+10; }}alert(aaa > 20); // truealert(+aaa); // 40alert(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!

var bbb = {i: 10,toString: function() {console.log('toString');return this.i;},valueOf: function() {console.log('valueOf');return this.i;}}alert(bbb);// 10 toStringalert(+bbb); // 10 valueOfalert(''+bbb); // 10 valueOfalert(String(bbb)); // 10 toStringalert(Number(bbb)); // 10 valueOfalert(bbb == '10'); // true valueOfalert(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.

var aa = {i: 10,toString: function() {console.log('toString');return this.i;}}alert(aa);// 10 toStringalert(+aa); // 10 toStringalert(''+aa); // 10 toStringalert(String(aa)); // 10 toStringalert(Number(aa)); // 10 toStringalert(aa == '10'); // true toString

Let's look at valueOf.

var bb = {i: 10,valueOf: function() {console.log('valueOf');return this.i;}}alert(bb);// [object Object]alert(+bb); // 10 valueOfalert(''+bb); // 10 valueOfalert(String(bb)); // [object Object]alert(Number(bb)); // 10 valueOfalert(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.

Object.prototype.toString = null;var cc = {i: 10,valueOf: function() {console.log('valueOf');return this.i;}}alert(cc);// 10 valueOfalert(+cc); // 10 valueOfalert(''+cc); // 10 valueOfalert(String(cc)); // 10 valueOfalert(Number(cc)); // 10 valueOfalert(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.

The above is a full description of the valueOf and toString methods in JavaScript. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.