aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/function/displayname/index.html
blob: 4fab613040b16b512eb9fca2d2685d85c48dff1a (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
---
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="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}