In JavaScript, The toString () method and the alueOf () method are used to convert an object to a basic data type or string. When converting an object to a string: 1. call the toString () method of the object to convert the object to the returned string. If the method returns other basic data types, it will... syntaxHighlighter. in JavaScript, The toString () method and the alueOf () method are used to convert an object to a basic data type or string.
When converting an object to a string:
1. Call the toString () method of the object to convert the object to the returned string. If the method returns other basic data types, it is automatically converted to a string;
2. If the object does not have the toString () method, or the method returns a non-basic data type, call the valueOf () method in the same way;
3. Similarly, if the valueOf () method does not exist or the returned value is not of the basic data type, an error is prompted (IE may not report an error );
Js Code
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
// Normally, call the toString () method first.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj); // The result is 123.
// The type returned by the toString () method is incorrect. Call the valueOf () method.
MyObject. prototype. toString = function () {return new Date ();};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj); // The result is 321.
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj); // the error message is displayed. ([Object] is not reported in IE8.)
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
// Normally, call the toString () method first.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj); // The result is 123.
// The type returned by the toString () method is incorrect. Call the valueOf () method.
MyObject. prototype. toString = function () {return new Date ();};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj); // The result is 321.
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj); // the error message is displayed. ([Object] is not reported in IE8.)
Similarly, when an object is converted to a basic data type, the corresponding processing is performed, but the valueOf () function is called first instead of toString ().
When converting an object to a basic data type:
1. Call the valueOf () method of the object to convert the object to the basic data type returned;
2. If the object does not have the valueOf () method, or the method returns a non-basic data type, call the toString () method in the same way;
3. Similarly, if the toString () method does not exist or the return value is not of the basic data type, an error is prompted;
Js Code
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
// Normally, call the valueOf () method first.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj * 2); // The result is 642.
// The valueOf () method returns an incorrect type. Call the toString () method.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj * 2); // The result is 246.
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj * 2); // error message
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
// Normally, call the valueOf () method first.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj * 2); // The result is 642.
// The valueOf () method returns an incorrect type. Call the toString () method.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj * 2); // The result is 246.
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj * 2); // error message
Pay special attention to the design of the "+" Operator for object operations, because "+" can both perform arithmetic operations and String concatenation operations.
1. When the two operands of "+" have the object type, convert the object to the basic data type according to the above rules;
2. If one of the converted two operands is of the string type, convert the other operand to a string;
3. Otherwise, convert both operands to the numeric type (or NaN) for addition.
Js Code
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
// Normally, call the valueOf () method and return a number.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj + 1); // The result is 322 (Number Addition)
Alert (myObj + "1"); // The result is 3211 (String concatenation)
// Normally, call the valueOf () method first to return the string
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return "321 ";};
Alert (myObj + 1); // The result is 3211 (String concatenation)
Alert (myObj + "1"); // The result is 3211 (String concatenation)
// The valueOf () method returns an incorrect type. Call toString () to return a string.
MyObject. prototype. toString = function () {return "123 ";};
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj + 1); // The result is 1231 (String concatenation)
Alert (myObj + "1"); // The result is 1231 (String concatenation)
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj + 1); // The system prompts an error (IE8 does not report an error and the system prompts undefined)
Alert (myObj + "1"); // The system prompts an error (IE8 does not report an error and the system prompts undefined)
// Custom function (class)
Function myObject (objectName)
{
This. objectName = objectName;
}
Var myObj = new myObject ("MyObj ");
Www.2cto.com
// Normally, call the valueOf () method and return a number.
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return 321 ;};
Alert (myObj + 1); // The result is 322 (Number Addition)
Alert (myObj + "1"); // The result is 3211 (String concatenation)
// Normally, call the valueOf () method first to return the string
MyObject. prototype. toString = function () {return 123 ;};
MyObject. prototype. valueOf = function () {return "321 ";};
Alert (myObj + 1); // The result is 3211 (String concatenation)
Alert (myObj + "1"); // The result is 3211 (String concatenation)
// The valueOf () method returns an incorrect type. Call toString () to return a string.
MyObject. prototype. toString = function () {return "123 ";};
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj + 1); // The result is 1231 (String concatenation)
Alert (myObj + "1"); // The result is 1231 (String concatenation)
// The toString () method does not exist. The valueOf () method returns an incorrect type.
MyObject. prototype. toString = undefined;
MyObject. prototype. valueOf = function () {return new Date ();};
Alert (myObj + 1); // The system prompts an error (IE8 does not report an error and the system prompts undefined)
Alert (myObj + "1"); // The system prompts an error (IE8 does not report an error and the system prompts undefined)
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.