We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

这篇翻译不完整。请帮忙从英语翻译这篇文章

非标准
该特性是非标准的,请尽量不要在生产环境中使用它!

function.displayName属性获取函数的显示名称

Description 描述

当一个函数的displayName属性被定义,displayName属性将返回函数的显示名称。

function doSomething() {}

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

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

popup.displayName = 'Show Popup';

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

可以在函数表达式重定义函数的显示名称function expression:

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

可以动态修改函数的显示名称:

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

Examples 例子

这个显示名称通常在控制台和配置文件中,用它来提醒对它背后的真实函数名 func.name的引用。例如:

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

a; // "function My Function()"

Specifications 规范

不属于任何规范

Browser compatibility 浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? 13 (13) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ?

文档标签和贡献者

此页面的贡献者: minstrel1977, webery
最后编辑者: minstrel1977,