Home > Web Front-end > JS Tutorial > Type conversion of JavaScript objects

Type conversion of JavaScript objects

高洛峰
Release: 2016-11-25 15:31:40
Original
1085 people have browsed it

In JavaScript, the toString() method and alueOf() method are used to convert objects into basic data types or strings.



When converting an object into a string:

1. Call the toString() method of the object to convert the object into the returned string. If the method returns other basic data types, it will be automatically converted into a string;

2. If the object does not have a toString() method, or the method returns is not a basic data type, then call the valueOf() method in the same way;

3. Similarly, if the valueOf() method does not exist or the return value is not a basic data type , an error will be prompted (IE may not report an error);

Js code
//Custom function (class)
function myObject(objectName)
{
This.objectName = objectName;
}
var myObj = new myObject(" MyObj");

//Normally, first call the toString() method
myObject.prototype.toString = function(){return 123;};
myObject.prototype.valueOf = function(){return 321;};
alert(myObj); //The result is 123

//The toString() method returns an incorrect type, 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); // Prompt error (no error is reported in IE8 and prompt [Object])

//Custom function ( class)
function myObject(objectName)
{
This.objectName = objectName;
}
var myObj = new myObject("MyObj");

//Normally, first call the toString() method
myObject.prototype.toString = function(){return 123;};
myObject.prototype.valueOf = function(){return 321;};
alert(myObj); //The result is 123

//The toString() method returns an incorrect type, Call the valueOf() method
myObject.prototype.toString = function(){return new Date();};
myObject.prototype.valueOf = function(){return 321;};
alert(myObj); //The result is 321

//toString() method does not exist, valueOf() method returns incorrect type
myObject.prototype.toString = undefined;
myObject.prototype.valueOf = function(){return new Date();};
alert (myObj); //Prompt error (no error is reported in IE8 and prompts [Object])

Similarly, the corresponding processing will be done when converting the object into a basic data type, but the valueOf() function will be called first instead of toString( ).

When converting an object into a basic data type:

1. Call the valueOf() method of the object to convert the object into the returned basic data type;

2. If the object does not have a valueOf() method, or the method returns something other than Basic data type, then call the toString() method in the same way;

3. Similarly, if the toString() method does not exist or the return value is not a basic data type, an error will be prompted;



Js code
//Customized Function (class)
function myObject(objectName)
{
this.objectName = objectName;
}
var myObj = new myObject("MyObj");

//Normally, first call the valueOf() method
myObject.prototype .toString = function(){return 123;};
myObject.prototype.valueOf = function(){return 321;};
alert(myObj*2); //The result is 642

//The valueOf() method returns Incorrect type, call toString() method
myObject.prototype.toString = function(){return 123;};
myObject.prototype.valueOf = function(){return new Date();};
alert(myObj*2 ; );};
alert(myObj*2); "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);                                                                                                                                                                               . prototype.toString = undefined;
myObject.prototype.valueOf = function(){return new Date();};
alert(myObj*2); Pay special attention to this, because "+" can perform both arithmetic operations and string concatenation.

1. When the two operands of "+" have object types, first convert the objects into basic data types according to the above rules;

2. If one of the two converted operands is of string type , then convert the other operand into a string;

3. Otherwise, convert both operands into numeric types (or NaN) and perform the addition operation.



Js code
//Custom function (class)
function myObject(objectName)
{
this.objectName = objectName;
}
var myObj = new myObject("MyObj");

//Normal situation , first call the valueOf() method and return the number
myObject.prototype.toString = function(){return 123;};
myObject.prototype.valueOf = function(){return 321;};
alert(myObj + 1); //The result is 322 (added values)
alert(myObj + "1"); 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, calls the toString() method, and returns 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 splicing)

//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); No error is reported, and prompts undefined)

//Custom function (class)
function myObject(objectName)
{
This.objectName = objectName;
}
var myObj = new myObject("MyObj");
www.2cto .com
//Under normal circumstances, first 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 (added values)
alert(myObj + "1"); , return the string
myObject.prototype.toString = function(){return 123;};
myObject.prototype.valueOf = function(){return "321";};
alert(myObj + 1); 3211 (string concatenation)
alert(myObj + "1");                                    //The result is 3211 (string concatenation)
 
//The valueOf() method returns an incorrect type. Call the toString() method 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"); () method does not exist, valueOf() method returns incorrect type
myObject.prototype.toString = undefined;
myObject.prototype.valueOf = function(){return new Date();};
alert(myObj + 1); //Prompt an error (IE8 does not report an error, and prompts undefined)
alert(myObj + "1");

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template