English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
call()은 하나의 객체에 속한 함수를 다른 객체에 적용할 수 있게 합니다./또는 다른 객체에 할당된 메서드를 호출합니다.
function Product(name, price) { this.name = name; this.price = price; } function Food(name, price) { Product.call(this, name, price); this.category = "food"; } document.write(new Food("cheese", 12).name);테스트를 보고‹/›
예제에서 call() 함수에 대한/이 메서드는 새로운 값 this를 제공합니다. 호출함으로써 한 번의 메서드를 작성하고, 다른 객체에서该方法을 상속할 수 있으며, 새로운 객체에 대해 메서드를 다시 작성하지 않아도 됩니다.
call()를 사용하여 객체의 생성자를 연결할 수 있습니다. Java와 유사합니다.
function Product(name, price) { this.name = name; this.price = price; } function Food(name, price) { Product.call(this, name, price); this.category = "food"; } function Toy(name, price) { Product.call(this, name, price); this.category = "toy"; } let cheese = new Food("cheese", 12); let robot = new Toy("robot", 85);테스트를 보고‹/›
아래 예제에서는 매개변수를 전달하지 않고 display 함수를 호출했습니다:
var name = "Seagull"; function display() { document.write(this.name); } display.call();테스트를 보고‹/›