HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
💌
JJong’s Archive
/
🌞
JS
/
function의 메서드: call, apply

function의 메서드: call, apply

Status
In progress
Tags
날짜
Nov 17, 2023 11:05 PM
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