자바스크립트 함수 호출
메소드 재사용
메서드를 사용하면 다른 개체에 사용할 수 있는 call()
메서드를 작성할 수 있습니다.
모든 함수는 메서드입니다
JavaScript에서 모든 함수는 객체 메서드입니다.
함수가 JavaScript 객체의 메소드가 아닌 경우 전역 객체의 함수입니다(이전 장 참조).
아래 예에서는 firstName, lastName, fullName의 3가지 속성이 있는 개체를 만듭니다.
예시
const myObject = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
// This will return "John Doe":
myObject.fullName();
이 키워드 _
함수 정의에서 함수 this
의 "소유자"를 나타냅니다.
위의 예에서 는 fullName 함수 를 "소유"하는
this
사람 개체 입니다.
즉, this.firstName 은 이 객체 의 firstName 속성을 의미 합니다 .
JS this Keywordthis
에서 키워드에 대해 자세히 알아보세요 .
자바스크립트 call() 메소드
call()
메소드는 사전 정의된 JavaScript 메소드입니다 .
소유자 개체를 인수(매개변수)로 사용하여 메서드를 호출(호출)하는 데 사용할 수 있습니다.
를 사용 call()
하면 개체가 다른 개체에 속한 메서드를 사용할 수 있습니다.
이 예 에서는 person1 에서 이를 사용하여 person 의 fullName 메서드 를 호출합니다 .
예시
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}
// This will return "John Doe":
person.fullName.call(person1);
이 예 에서는 person2 에서 이를 사용하여 person 의 fullName 메서드 를 호출합니다 .
예시
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}
// This will return "Mary Doe"
person.fullName.call(person2);
인수가 있는 call() 메서드
이 call()
메서드는 인수를 받아들일 수 있습니다.
예시
const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName
+ "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.call(person1, "Oslo", "Norway");