ES5
작성
Function.prototype.call()
call('this값, 인자1, 인자2, ...)this값을 재할당하여 메소드를 실행시킬 대 사용된다.
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
console.log(new Food('cheese', 5));
// Food {name: 'cheese', price: 5, category: 'food'}
Function.prototype.apply()
call함수와 apply함수의 차이: 매개변수 전달 방식의 차이
call('this값, [인자1, 인자2, ...])
func.call(thisArg[, arg1[, arg2[, ...]]])
// call함수는 매개변수를 나열해서 전달한다.
func.apply(thisArg, [argsArray])
// apply함수는 매개변수를 배열로 전달한다.