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

Non-standard
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.

Properti function.displayName mengembalikan nama fungsi.

Deskripsi

Ketika didefinisikan, properti displayName mengembalikan nama dari sebuah fungsi:

function doSomething() {}

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

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

popup.displayName = 'Show Popup';

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

Anda bisa mendefinisikan sebuah fungsi dengan sebuah display nama di 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

Anda bisa merubah  displayName dari sebuah fungsi secara dinamis:

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

Contoh

Biasanya lebih banyak digunakan dengan console dan profiler melalui func.name untuk menampilkan nama function.

Dengan menuliskan kode berikut, maka seharusnya akan menampilkan "function My Function()":

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

a; // "function My Function()"

Spesifikasi

Bukan bagian dari spesifikasi.

Kompabilitas browser

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

Tag Dokumen dan Kontributor

Kontributor untuk laman ini: bekti
Terakhir diperbarui oleh: bekti,