valueof function and ToString method in JavaScript

Source: Internet
Author: User

Basically, all JS data types have two methods, valueof and ToString, except for null. They both solve the problem of JavaScript value operation and display, this article will be described in detail, the need for friends can refer to the following JavaScript valueof function method 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.
The ValueOf method definition differs for each JavaScript intrinsic object.
Object return value
Array The elements of the array are converted to strings, which are 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 stored time is the number of milliseconds that are counted from midnight January 1, 1970 UTC.
Function function itself.
Number numeric value.
Object The object itself. This is the default condition.
String The string value.

The Math and Error objects do not have a valueOf method.

Basically, all JS data types have two methods, valueof and ToString, except for null. They both solve the problem of JavaScript value arithmetic and display.
JavaScript's ValueOf () method
The ValueOf () method can return the original value of a Boolean object.
Usage booleanobject.valueof (), the return value is the original Boolean value of Booleanobject. If the object calling the method is not a Boolean, an exception TypeError is thrown.
The code is as follows:
<script type= "Text/javascript" >
var boo = new Boolean (false);
document.write (Boo.valueof ());
</script>
The above script will output false.
The ToString () method of JavaScript
The ToString () method converts a logical value to a string and returns the result.
Usage booleanobject.tostring (), which 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 called automatically when a Boolean object is used in a string environment.
The following script creates a Boolean object and converts it to a string:
The code is as follows:
<script type= "Text/javascript" >
var boo = new Boolean (true);
document.write (Boo.tostring ());
</script>
Script output: True.
first Look at a case
The code is 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 to differentiate which method is called, we can test it by another method. In the Console.log, please experiment with the FF with Firebug!
The code is 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 (number (BBB)); Ten ValueOf
Alert (bbb = = ' 10 '); True ValueOf
Alert (bbb = = = ' 10 '); False
At first glance, it is probably a feeling that if you call the ToString method 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.
The code is 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 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
If only ToString is overridden, the object conversion ignores the presence of valueof to convert. However, if only the ValueOf method is overridden, the valueof method is preferred when converting to a string. In the case where ToString cannot be called, only valueof can be made to battle. For that strange string stitching problem, it may be that the operator, opened Ecma262-5 found there is a getvalue operation. Well, then the answer should be uncovered. Overrides increase the optimization of their invocation, and in the case of operators, the valueof has a higher priority than the ToString.

valueof function and ToString method in JavaScript

Related Article

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.