Function object, NFE
The “name” property
// 函式物件有 name 屬性
function sayHi() {
alert("Hi");
}
alert(sayHi.name); // sayHi
// 用變數命名也可以
let sayHi = function() {
alert("Hi");
}
alert(sayHi.name); // sayHi (works!)
// 用預設值的方式有可以
function f(sayHi = function() {}) {
alert(sayHi.name); // sayHi (works!)
}
f();
// 物件的方法有有 name 屬性
let user = {
sayHi() {
// ...
},
sayBye: function() {
// ...
}
}
alert(user.sayHi.name); // sayHi
alert(user.sayBye.name); // sayBye
// 無法取得名字的情形
// function created inside array
let arr = [function() {}];
alert( arr[0].name ); // <empty string>
// the engine has no way to set up the right name, so there is noneThe “length” property
Custom properties
Named Function Expression
Last updated
Was this helpful?