A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/apply below:

handler.apply() - JavaScript | MDN

handler.apply()

Baseline Widely available

handler.apply() 메서드는 함수 호출에 대한 트랩입니다.

시도해 보기
function sum(a, b) {
  return a + b;
}

const handler = {
  apply: function (target, thisArg, argumentsList) {
    console.log(`Calculate sum: ${argumentsList}`);
    // Expected output: "Calculate sum: 1,2"

    return target(argumentsList[0], argumentsList[1]) * 10;
  },
};

const proxy1 = new Proxy(sum, handler);

console.log(sum(1, 2));
// Expected output: 3
console.log(proxy1(1, 2));
// Expected output: 30
구문
var p = new Proxy(target, {
  apply: function (target, thisArg, argumentsList) {},
});
매개 변수

다음 매개변수는 apply() 메소드에 전달됩니다. this는 핸들러에 바인딩됩니다.

target

호출할 수 있는 대상 객체

thisArg

호출에 대한 this 인수

argumentsList

호출에 대한 인수 목록

반환 값

apply() 메서드는 어떤 값이든 반환할 수 있습니다.

설명

handler.apply() 메서드는 함수 호출에 대한 트랩입니다.

가로채기

이 트랩은 다음 작업을 가로챌 수 있습니다.

불변 조건

다음 불변 조건이 위반되면 프록시는 TypeError를 발생시킵니다.

대상 객체는 그 자체로 호출이 가능해야합니다. 즉, 함수 객체여야 합니다.

예제 함수 호출 가로채기

다음 코드는 함수 호출을 트랩합니다.

const p = new Proxy(function () {}, {
  apply(target, thisArg, argumentsList) {
    console.log(`called: ${argumentsList}`);
    return argumentsList[0] + argumentsList[1] + argumentsList[2];
  },
});

console.log(p(1, 2, 3)); // "호출: 1,2,3"
// 6
명세서 브라우저 호환성 같이 보기

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4