(JS) 함수 객체의 기본 프로퍼티

호두파파·2021년 1월 24일
0

(JS) 함수

목록 보기
2/10

length 프로퍼티

함수 객체의 length프로퍼티는 함수를 작성할 때 정의한 인자 개수를 나타낸다.

function func2(x,y){
    return x + y;
}
function func3(x,y,z){
    return x + y + z;
}

console.log('func2.length - ' + func2.length);  // func2.length - 2
console.log('func3.length - ' + func3.length);  // func3.length - 3

prototype 프로퍼티

모든 함수는 객체로서 prototype프로퍼티를 가지고 있다. prototype프로퍼티는 모든 객체의 부모를 나타내는 내부 프로퍼티인 [[Prototype]]과는 다르다. 함수 객체가 가지는 prototype프로퍼티는 이 함수가 생성자로 사용될 때 이 함수를 통해 생서오딘 객체의 부모 역활을 하는 프토토타입 객체를 가리킨다.

prototype프로퍼티는 함수가 생성될 때 만들어지며, constructor 프로퍼티 하나만 있는 객체를 가리킨다. 그리고 프로토타입 객체의 유일한 constructor프로퍼티는 자신과 연결된 함수를 가리킨다.

즉, 자바스크립트에서는 함수를 생성할 때, 함수 자신과 연결된 프로토타입 객체를 동시에 생성되며, 각각 prototypeconstructor라는 프로퍼티로 서로를 참조하게 된다.

function myFunction(){
    return true;
}

console.dir(myFunction.prototype);
console.dir(myFunction.prototype.constructor);
profile
안녕하세요 주니어 프론트엔드 개발자 양윤성입니다.

0개의 댓글