Home > Web Front-end > JS Tutorial > body text

JavaScript method toString() converts a Date object into a string and returns the result

黄舟
Release: 2017-11-08 11:22:06
Original
4696 people have browsed it

Definition and usage

toString() method can convert a Date object into a string and return the result.

Syntax

dateObject.toString()
Copy after login

Return value

The string representation of dateObject, expressed in local time.

Example

In this example, we will convert today's date to a string:

<script type="text/javascript">

var d = new Date()
document.write (d.toString())

</script>
Copy after login

Output:

Wed Nov 08 2017 11:20:25 GMT+0800 (中国标准时间)
Copy after login

How to use toString() Convert today's date to a string.




<script type="text/javascript">

var d = new Date()
document.write (d.toString())

</script>


Copy after login

Result:

Wed Nov 08 2017 11:21:32 GMT+0800 (中国标准时间)
Copy after login

JavaScript’s toString() method
toString() method can convert a logical value into a string and return 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 on which this method is called is not a Boolean, an exception TypeError is thrown.
This method will be automatically called when a Boolean object is used in a string environment.
The following script will create a Boolean object and convert it into a string:

<script type="text/javascript"> 
var boo = new Boolean(true); 
document.write(boo.toString()); 
</script>
Copy after login

Script output:

true。
Copy after login

Let’s look at an example first:

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
Copy after login

The reason why This results because they secretly call the valueOf or toString methods. But how to distinguish which method is called under what circumstances? We can test it through another method. Since console.log is used, please experiment in FF with firebug installed!

var bbb = { 
i: 10, 
toString: function() { 
console.log(&#39;toString&#39;); 
return this.i; 
}, 
valueOf: function() { 
console.log(&#39;valueOf&#39;); 
return this.i; 
} 
} 
alert(bbb);// 10 toString 
alert(+bbb); // 10 valueOf 
alert(&#39;&#39;+bbb); // 10 valueOf 
alert(String(bbb)); // 10 toString 
alert(Number(bbb)); // 10 valueOf 
alert(bbb == &#39;10&#39;); // true valueOf 
alert(bbb === &#39;10&#39;); // false
Copy after login

At first glance, it seems that the toString method is called when converting to a string, and the valueOf method is called when converting to a numerical value, but two of them are very discordant. One is alert(''+bbb), string concatenation should call the toString method... The other one we can temporarily understand is ===operator does not perform implicit conversion, so they are not called. To find out the truth, we need more rigorous experiments.

var aa = { 
i: 10, 
toString: function() { 
console.log(&#39;toString&#39;); 
return this.i; 
} 
} 
alert(aa);// 10 toString 
alert(+aa); // 10 toString 
alert(&#39;&#39;+aa); // 10 toString 
alert(String(aa)); // 10 toString 
alert(Number(aa)); // 10 toString 
alert(aa == &#39;10&#39;); // true toString 
再看valueOf。 
var bb = { 
i: 10, 
valueOf: function() { 
console.log(&#39;valueOf&#39;); 
return this.i; 
} 
} 
alert(bb);// [object Object] 
alert(+bb); // 10 valueOf 
alert(&#39;&#39;+bb); // 10 valueOf 
alert(String(bb)); // [object Object] 
alert(Number(bb)); // 10 valueOf 
alert(bb == &#39;10&#39;); // true valueOf 
发现有点不同吧?!它没有像上面toString那样统一规整。对于那个[object Object],我估计是从Object那里继承过来的,我们再去掉它看看。 
Object.prototype.toString = null; 
var cc = { 
i: 10, 
valueOf: function() { 
console.log(&#39;valueOf&#39;); 
return this.i; 
} 
} 
alert(cc);// 10 valueOf 
alert(+cc); // 10 valueOf 
alert(&#39;&#39;+cc); // 10 valueOf 
alert(String(cc)); // 10 valueOf 
alert(Number(cc)); // 10 valueOf 
alert(cc == &#39;10&#39;); // true valueOf
Copy after login

If only toString is rewritten, the object will be converted regardless of the existence of valueOf. However, if only the valueOf method is overridden, the valueOf method will be given priority when converting to a string. When toString cannot be called, valueOf can only be used. Regarding the strange string splicing problem, it may be due to the operator. I opened ECMA262-5 and found that there is a getValue operation. Well, then the mystery should be solved. Rewriting will increase the optimization of their calls, and in the case of operators, the priority of valueOf is inherently higher than that of toString.

The above is the detailed content of JavaScript method toString() converts a Date object into a string and returns the result. For more information, please follow other related articles on the PHP Chinese website!

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