valueof function and ToString method in JavaScript to understand _javascript skills

Source: Internet
Author: User
Tags object object
The ValueOf function method in JavaScript is to return the original value of the specified object. How to use:
Object.valueof () object is a required option parameter is any intrinsic JScript object.
Each JavaScript intrinsic object has a different valueof method definition.
Object return value
Array The elements of the array are converted to strings, separated by commas, and concatenated together. The operation is the same as the array.tostring and Array.join methods.
Boolean A Boolean value.
Date The time to store is UTC, which is counted in milliseconds from midnight January 1, 1970.
Function The function itself.
Number numeric value.
Object Object itself. This is the default condition.
String The string value.

The Math and Error objects do not have valueof methods.

Basically, all JS data types have both valueof and ToString methods, except NULL. Both of them solve the problem of JavaScript value operation and display.
the ValueOf () method of JavaScript
The valueof () method returns the original value of a Boolean object.
Use Booleanobject.valueof () to return the original Boolean value of Booleanobject. If the object calling the method is not a Boolean, an exception typeerror is thrown.
Copy Code code as follows:

<script type= "Text/javascript" >
var boo = new Boolean (false);
document.write (Boo.valueof ());
</script>

The above script will output false.
JavaScript's ToString () method
The ToString () method converts a logical value to a string and returns the result.
Usage booleanobject.tostring (), the return value returns the string "true" or "false" based on the original Boolean value or the value of the Booleanobject object. If the object calling the method is not a Boolean, an exception typeerror is thrown.
This method is invoked automatically when a Boolean object is used in a string environment.
The following script creates a Boolean object and converts it to a string:
Copy Code code as follows:

<script type= "Text/javascript" >
var boo = new Boolean (true);
document.write (Boo.tostring ());
</script>

Script output: True.
first Look at a case
Copy Code code as follows:

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 do you tell which method is invoked, and we can test it in another way. Because of the use of Console.log, please experiment in the FF containing Firebug!
Copy Code code as follows:

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 (BBB); Ten valueof
Alert (bbb = = ' 10 '); True valueof
Alert (bbb = = ' 10 '); False

At first glance, the feeling is that if you call the ToString method when you convert to a string, you call the ValueOf method when you convert to a numeric value, but two of them are very discordant. One is alert ("+BBB"), and the string combination should be called the ToString method ... Another we can temporarily understand is that the = = = Operator does not convert implicitly, and therefore does not call them. In order to investigate the truth, we need more rigorous experiments.
Copy Code code as follows:

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]
alert (+BB); Ten valueof
Alert (' +bb); Ten valueof
Alert (String (BB)); [Object Object]
Alert (number (BB)); Ten valueof
Alert (BB = = ' 10 '); True valueof
Find it a little different?! It is not as uniform and regular as the above ToString. For that [object], I guess I inherited it 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);//Ten valueof
alert (+CC); Ten valueof
Alert (' +cc); Ten valueof
Alert (String (cc)); Ten valueof
Alert (number (cc)); Ten valueof
alert (cc = = ' 10 '); True valueof

If only ToString is overridden, object conversion ignores the presence of valueof for conversion. However, if you override the ValueOf method only, the ValueOf method is preferred when you want to convert to a string. In the case of cannot call ToString, only let valueof battle. For that strange string concatenation problem, it may be out of the operator, turn ecma262-5 found there is a getvalue operation. Well, then the answer should be solved. Overrides increase the optimization of their invocation and, in the case of an operator, the valueof priority is inherently higher than the 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.