call: 주어진
this
값 및 각각 전달된 인수목록과 함께 함수를 호출하는 메소드func.call(thisArg[, arg1[, arg2[, ...]]])
- 반환값:
this
와 arguments 를 매개로 호출된 함수의 반환값
- 호출되는 함수 내부에 사용되는 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).name); // Expected output: "cheese"
apply: 주어진
this
값 및 각각 전달된 인수의 배열와 함께 함수를 호출하는 메소드func.apply(thisArg, [argsArray]);
const numbers = [5, 6, 2, 3, 7]; const max = Math.max.apply(null, numbers); console.log(max); // Expected output: 7 const min = Math.min.apply(null, numbers); console.log(min); // Expected output: 2