非標準
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
function.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"
関数表現 内で、表示名と同時に関数を定義できます:
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
関数の displayName を動的に変更できます:
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)"
例
関数の名称は、func.name を通してコンソールやプロファイラに表示されます。
コンソールで次のテキストを入力すると、"function My Function()" のように表示されます:
var a = function() {};
a.displayName = 'My Function';
a; // "function My Function()"
仕様
どの仕様にも含まれていません。
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | ? | 13 (13) | ? | ? | ? |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | ? | ? | ? | ? | ? | ? |