JavaScript

javaScript-함수 객체의 프로퍼티

용용it 2023. 4. 15. 21:04

함수는 객체이므로 프로퍼티를 가질 수 있다.

브라우저 콘솔에서 console.dir 메서드를 사용하여 함수 객체의 내부를 볼 수 있다.

 

function square(number){
    return number * number;
}

console.dir(square) 

//
ƒ square(number)
    arguments: null
    caller: null
    length: 1
    name: "square"
    prototype: {constructor: ƒ}
    [[FunctionLocation]]: VM186:1
    [[Prototype]]: ƒ ()
    [[Scopes]]: Scopes[1]

 

함수 객체의 프로퍼티는 arguments, caller, length, name, __proto__접근자 프로퍼티, prototype 프로퍼티가 있다.


length 프로퍼티

 

함수 객체의 length 프로퍼티는 함수를 정의할 때 선언한 매개변수의 개수를 가리킨다.

function foo(){}
console.log(foo.length); // 0

function bar(x){
	return x;
}
console.log(bar.length); // 1

function baz(x, y) {
	return x * y; 
}
console.log(baz.length); // 2

arguments 객체의 length 프로퍼티와 함수 객체의 length 프로퍼티의 값은 다를 수 있기 때문에 주의해야한다.

arguments 객체의 length 프로퍼티는 인자의 개수를 가리키고,

함수 객체의 length 프로퍼티는 매개변수의 개수를 가리킨다.