현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.
This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
function.name 속성은 함수의 이름을 반환합니다.
Property attributes of Function.name |
|
|---|---|
| Writable | no |
| Enumerable | no |
| Configurable | yes |
Description
name속성은 함수의 이름을 반환하고, 익명함수의 경우 빈 문자열을 반환합니다.:
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 == ''); // also true
함수의 이름은 function expression 구문을 이용해 정의할 수 있습니다:
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 is not defined
함수의 이름은 정의된 후 변경할 수 없으며, 이 속성은 읽기전용입니다:
var object = {
// anonymous
someMethod: function() {}
};
object.someMethod.name = 'someMethod';
console.log(object.someMethod.name); // empty string, someMethod is anonymous
Examples
obj.constructor.name을 객체의 "class"를 확인하기 위해 사용할 수 있습니다:
function a() {}
var b = new a();
console.log(b.constructor.name); // logs "a"
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 6 (ECMA-262) The definition of 'name' in that specification. |
Release Candidate | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 33 | (Yes) | Not supported | (Yes) | (Yes) |
| Configurable: true | 43 | 38 (38) | ? | ? | ? |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | Not supported | (Yes) | (Yes) |
| Configurable: true | ? | ? | 38.0 (38) | ? | ? | ? |