blob: 6af5bd5d2d44bd1179295aa4cb16198d4bfe46f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
---
title: String.prototype.valueOf()
slug: Web/JavaScript/Reference/Global_Objects/String/valueOf
translation_of: Web/JavaScript/Reference/Global_Objects/String/valueOf
---
<div>{{JSRef}}</div>
<p><strong><code>valueOf()</code></strong> 方法返回 {{jsxref("String")}} 对象的原始值</p>
<div>{{EmbedInteractiveExample("pages/js/string-valueof.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate"><code><var>str</var>.valueOf()</code></pre>
<h3 id="返回结果">返回结果</h3>
<p>A string representing the primitive value of a given {{jsxref("String")}} object.</p>
<h2 id="描述">描述</h2>
<p>The <code>valueOf()</code> method of {{jsxref("String")}} returns the primitive value of a {{jsxref("String")}} object as a string data type. This value is equivalent to {{jsxref("String.prototype.toString()")}}.</p>
<p>此方法通常由JavaScript在内部调用,而不是在代码中显式调用。</p>
<h2 id="示例">示例</h2>
<h3 id="使用_valueOf">使用 <code>valueOf()</code></h3>
<pre class="brush: js notranslate">var x = new String('Hello world');
console.log(x.valueOf()); // Displays 'Hello world'
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.valueof', 'String.prototype.valueOf')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.String.valueOf")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>{{jsxref("String.prototype.toString()")}}</li>
<li>{{jsxref("Object.prototype.valueOf()")}}</li>
</ul>
|