非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。

function .displayName property returns the display name of the function.

范例

Setting a displayName

It is usually preferred by consoles and profilers over func.name to display the name of a function.

By entering the following in a console, it should display as something like " function My Function() ":

var a = function() {};
a.displayName = 'My Function';
a; // "function My Function()"
					

When defined, the displayName property returns the display name of a function:

function doSomething() {}
console.log(doSomething.displayName); // "undefined"
var popup = function(content) { console.log(content); };
popup.displayName = 'Show Popup';
console.log(popup.displayName); // "Show Popup"
					

Defining a displayName in function expressions

You can define a function with a display name in a 函数表达式 :

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
					

Changing displayName dynamically

You can dynamically change the displayName of a function:

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)"
					

规范

Not part of any standard.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
displayName 非标 Chrome No Edge No Firefox 13 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 14 Opera Android No Safari iOS No Samsung Internet Android No nodejs No

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

另请参阅

元数据

  • 最后修改: