Basically, all data types in javascript have the two methods: valueOf and toString, except null. They solve the problems of JavaScript value calculation and display. This article will introduce in detail. Friends in need can refer to it.
toString()
The function of the toString() function is to return the string representation of the object. The default toString() method of object in JavaScript returns the string "[object Object]". A new toString() method can be implemented when defining a class, thus returning more readable results. JavaScript defines a more readable toString() method for array objects, function objects, regular expression objects and Date date objects:
The toString() method will return comma-separated array members. For example, [1,2,3].toString() will return the string "1,2,3".
The toString() method will return the text definition of the function. For example, (function(x){return x*2;}).toString() will return the string "function(x){return x*2;}".
The toString() method of function is similar to the toString() method, which will return the text definition of the regular expression. For example, /\d+/() will return the string "/\\d+/g".
The toString() method will return a readable datetime string.
5. If the Boolean value is true, return "true". Otherwise, return "false".
valueOf()
The function of the valueOf() function is to return the object itself. Like toString(), a new valueOf() method can be implemented when defining a class, thus returning the required result. JavaScript defines a more readable valueOf() method for Date objects:
The valueOf() method returns a timestamp value, which is the time difference (in milliseconds) between the Date object and 0:00 on January 1, 1970. The other returns the object itself.
There is a piece of code inside the js elevation:
var colors = ["red", "blue", "green"]; // Create an array containing 3 stringsalert(()); // red,blue,green alert(()); // red,blue,green alert(colors); // red,blue,green
All three outputs are the same, so what is the difference between toString() and valueOf()? See the next example I wrote:
var arr = [1,2,3]; alert((())); alert((()));
The result is that the first one is true and the second one is false. Why? In fact, after valueOf() is called, it still returns an array. When this array is alerted, the toString() function will be called, so it is not that the valueOf() and toString() functions are the same, but the toString() function is indirectly called!
Further tests:
var arr = [1,2,3]; = function () { alert("You called the toString function"); } alert(());
The result is that we will see "You called the toString function".
For numeric values, we can directly obtain the numbers when calling valueOf, and do not need to be converted into strings, so we will not call toString. In other words, if we need to obtain the string form of the operation object, we will call its toString function.
Verify the following code:
var bbb = { i: 10, toString: function() { ('toString'); return ; }, valueOf: function() { ('valueOf'); return ; } } alert(bbb);// 10 toString alert(+bbb); // 10 valueOf alert(''+bbb); // 10 valueOf alert(String(bbb)); // 10 toString alert(Number(bbb)); // 10 valueOf alert(bbb == '10'); // true valueOf alert(bbb === '10'); // false
My understanding: valueOf means returning the original value that best suits the object type, while toString returns the original value of the object type as a string.
The first
alert(bbb);// 10 toString
Here our alert function needs to be a string, so we get a string, not the original value, so toString is called
The second one
alert(+bbb); // 10 valueOf
Similarly, what alert wants is that the string is not the original value, but actually +bbb is called toString, while bbb is called valueOf
To verify that we write this
var a = { i: 1, valueOf: function () { alert("You called a valueOf function"); return ; }, toString: function () { alert("You called a toString function"); return ; } }; var c = { i: +a, valueOf: function () { alert("You called the valueOf function of c"); return ; }, toString: function () { alert("You called the toString function of c"); return ; } }; alert(c);
Let c=+a, then you can know the result. As expected, the valueOf of a and the toString of c are called
Third
alert(''+bbb); // 10 valueOf
Similarly, we can change c:+a in the previous program I wrote to c:''+a
The fourth
alert(String(bbb)); // 10 toString
The cast String actually calls the toString function with the incoming parameter inside it...
The fifth
alert(Number(bbb)); // 10 valueOf
There is a difference in this, because the i property of bbb is of numerical type. If i is a string like 11111xxxxx, we can see that the toString of bbb is called.
Code example:
var c = { i: "11111xxxx", valueOf: function () { alert("You called the valueOf function of c"); return ; }, toString: function () { alert("You called the toString function of c"); return ; } }; alert(c);
Sixth
alert(bbb == '10'); // true valueOf
The order of determining the equality in this is to obtain the original value and then determine whether the original values on both sides are equal, so call valueOf
The seventh is the last one
alert(bbb === '10'); // false
===The operator does not perform implicit conversion. The first step to determine the congruent is to determine the type. Because the types are different, nothing will be called later.
Summary: valueOf tends to operate, and toString tends to display.
1. When strongly converting string types, the toString method will be called first, and when strongly converting to numbers, the valueOf will be called first.
2. In the case of operation operators, the priority of valueOf is higher than that of toString.
Let me briefly explain the difference between valueof and tostring.
valueOf(): Returns the original value that best suits the object type;
toString(): Returns the original value of the object as a string.
These two methods are generally implicitly called by JS to meet different operational situations.
In numerical operations, valueOf() will be called first, such as a + b;
In string operations, toString() will be called first, such as alert(c).
The above is the valueOf and toString in Javascript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!