我试图理解为什么你会在字符串上使用 toString() 。
tutorialspoint.com 给出了这个例子。
<html>
<head>
<title>JavaScript String toString() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "Apples are round, and Apples are Juicy.";
document.write(str.toString( ));
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么不直接使用
document.write(str);
Run Code Online (Sandbox Code Playgroud)
toString()
当调用“纯”sring 时,方法没有任何实际意义,例如"Apples are round, and Apples are Juicy."
.
当您需要获取某些非字符串值的字符串表示时,它可能很有用。
// Example:
var x = new String(1000); // converting number into a String object
console.log(typeof x); // object
console.log(typeof x.toString()); // string
console.log(x.toString()); // "1000"
Run Code Online (Sandbox Code Playgroud)
的字符串对象覆盖的toString()的方法的对象 的对象; 它不继承Object.prototype.toString()。对于 String 对象,toString()方法返回对象的字符串表示,与String.prototype.valueOf()方法相同。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString
归档时间: |
|
查看次数: |
3841 次 |
最近记录: |