Function: name
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2016ë 8ì.
Function ì¸ì¤í´ì¤ì name ìì±ì í¨ìê° ìì±ë ë ì§ì ë ëë¡ í¨ìì ì´ë¦ì ëíë´ê±°ë ìµëª
ì¼ë¡ ìì±ë í¨ìì ê²½ì° anonymous ëë ''(ë¹ ë¬¸ìì´)ì¼ ì ììµëë¤.
ìëí´ ë³´ê¸°
const func1 = function () {};
const object = {
func2: function () {},
};
console.log(func1.name);
// Expected output: "func1"
console.log(object.func2.name);
// Expected output: "func2"
ê°
문ìì´.
Property attributes of Function: name | |
|---|---|
| ì°ê¸° ê°ë¥ | ë¶ê°ë¥ |
| ì´ê±° ê°ë¥ | ë¶ê°ë¥ |
| ì¤ì ê°ë¥ | ê°ë¥ |
ì°¸ê³ :
ë¹íì¤, ES6 ì´ì 구íììë configurable ìì± ëí false ììµëë¤.
ì¤ëª
name ìì±ì í¨ì ì´ë¦ ëë (ES6 구í ì´ì ) ìµëª
(anonymous) í¨ìì ëí´ìë ë¹ ë¬¸ìì´ì ë°íí©ëë¤.
function doSomething() {}
console.log(doSomething.name); // logs "doSomething"
new Function(...) ëë ê·¸ë¥ Function(...) 구문ì¼ë¡ ìì±ë í¨ìë name ìì±ì ë¹ ë¬¸ìì´ë¡ ì¤ì í©ëë¤. ë¤ì ìììë ìµëª
í¨ìê° ìì±ëë¯ë¡ nameì ë¹ ë¬¸ìì´ì ë°íí©ëë¤:
var f = function () {};
var object = {
someMethod: function () {},
};
console.log(f.name == ""); // true
console.log(object.someMethod.name == ""); // ìì true
ES6 í¨ì를 구íí ë¸ë¼ì°ì ë ìµëª í¨ì ì´ë¦ì ê·¸ 구문ì ìì¹ë¡ë¶í° ì¶ì¸¡í ì ììµëë¤. ì를 ë¤ì´:
var f = function () {};
console.log(f.name); // "f"
function ììì ì´ë¦ì¼ë¡ í¨ì를 ì ìí ì ììµëë¤:
var object = {
someMethod: function object_someMethod() {},
};
console.log(object.someMethod.name); // logs "object_someMethod"
try {
object_someMethod;
} catch (e) {
console.log(e);
}
// ReferenceError: object_someMethodê° ì ìëì§ ìì
í¨ì ì´ë¦ì ë°ê¿ ì ììµëë¤, ì´ ìì±ì ì½ê¸° ì ì©ì ëë¤:
var object = {
// ìµëª
someMethod: function () {},
};
object.someMethod.name = "someMethod";
console.log(object.someMethod.name); // ë¹ ë¬¸ìì´, someMethodë ìµëª
ê·¸ë¬ë ë°ê¾¸ë ¤ë©´, Object.defineProperty()를 ì¬ì©í ì ììµëë¤.
ì
ê°ì²´ì 'class'를 íì¸í기 ìí´ obj.constructor.nameì ì¬ì©í ì ììµëë¤:
function a() {}
var b = new a();
console.log(b.constructor.name); // logs "a"
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-function-instances-name> |