aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/uneval/index.html
blob: 234b683d48d895c9bfe9b23fd300309fa90232ef (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
59
60
61
62
63
64
65
66
---
title: uneval()
slug: Web/JavaScript/Reference/Global_Objects/uneval
tags:
  - JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/uneval
---
<div>{{jsSidebar("Objects")}}{{Non-standard_header}}</div>

<p><code><strong>uneval()</strong></code> 函数创建一个代表对象的源代码的字符串。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">uneval(<var>object</var>)</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>object</code></dt>
 <dd>JavaScript 表达式或者语句</dd>
</dl>

<h3 id="返回值">返回值</h3>

<dl>
 <dd>表示给定对象的源代码的字符串</dd>
</dl>

<div class="note"><strong>注意:</strong>无法通过此方法获取对象的 JSON 形式。</div>

<h2 id="描述">描述</h2>

<p><code>uneval()</code> 是一个顶级函数并且不与任何对象关联</p>

<h2 id="实例">实例</h2>

<pre class="brush:js">var a = 1;
uneval(a); // returns a String containing 1

var b = "1";
uneval(b) // returns a String containing "1"

uneval(function foo(){}); // returns "(function foo(){})"


var a = uneval(function foo(){return 'hi'});
var foo = eval(a);
foo(); // returns "hi"
</pre>

<h2 id="规范">规范</h2>

<p>不属于任何规范的一部分。</p>

<h2 id="浏览器兼容">浏览器兼容</h2>

<p>{{Compat("javascript.builtins.uneval")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Global_Objects/eval", "eval()")}}</li>
 <li>{{jsxref("JSON.stringify")}}</li>
 <li>{{jsxref("JSON.parse")}}</li>
 <li>{{jsxref("Object.toSource")}}</li>
</ul>