Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Function.displayName

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

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

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) {
    this.displayName = 'someMethod (' + value + ')';
  }
};

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

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

Examples 例子

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

在控制台输入下面的代码,控制台打印出 "function My Function()":

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

a; // "function My Function()"

Specifications 规范

不属于任何规范

Browser compatibility 浏览器兼容性

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 ? ? ? ? ? ?

文档标签和贡献者

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