aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/function/displayname/index.html
blob: abf8452f936eee07dc9c7ec5e7e1e9d42e515b6f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: Function.displayName
slug: Web/JavaScript/Reference/Global_Objects/Function/displayName
translation_of: Web/JavaScript/Reference/Global_Objects/Function/displayName
---
<div>{{JSRef}} {{non-standard_header}}</div>

<p><strong><code>function.displayName</code></strong> 属性获取函数的显示名称</p>

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

<p>当一个函数的 <code>displayName</code> 属性被定义,这个函数的 <code>displayName</code> 属性将返回显示名称。</p>

<pre class="brush: js">function doSomething() {}

console.log(doSomething.displayName); // "undefined"

var popup = function(content) { console.log(content); };

popup.displayName = 'Show Popup';

console.log(popup.displayName); // "Show Popup"
</pre>

<p>可以在函数表达式重定义函数的显示名称{{jsxref("Functions", "function expression", "", 1)}}:</p>

<pre class="brush: js">var object = {
  someMethod: function() {}
};

object.someMethod.displayName = 'someMethod';

console.log(object.someMethod.displayName); // logs "someMethod"

try { someMethod } catch(e) { console.log(e); }
// ReferenceError: someMethod is not defined
</pre>

<p>可以动态修改函数的显示名称:</p>

<pre class="brush: js">var object = {
  // anonymous
  someMethod: function(value) {
    arguments.callee.displayName = 'someMethod (' + value + ')';
  }
};

console.log(object.someMethod.displayName); // "undefined"

object.someMethod('123')
console.log(object.someMethod.displayName); // "someMethod (123)"
</pre>

<h2 id="Examples_例子">Examples 例子</h2>

<p>这个显示名称通常在控制台和配置文件中,用它来提醒对它背后的真实函数名 {{jsxref("Function.name", "func.name")}}的引用。例如:</p>

<p>通过如下的举例,显示的名称应该显示像"function My Function()"</p>

<pre class="brush: js">var a = function() {};
a.displayName = 'My Function';

a; // "function My Function()"</pre>

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

<p>不属于任何规范</p>

<h2 id="Browser_compatibility_浏览器兼容性">Browser compatibility 浏览器兼容性</h2>

<p>如果你愿意贡献数据,请访问<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>并同时给我们发送推送请求。</p>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(13)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>