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/get below:

handler.get() - JavaScript | MDN

handler.get()

Baseline Widely available

handler.get() 메서드는 속성 값을 가져오기 위한 트랩입니다.

시도해 보기
const monster1 = {
  secret: "easily scared",
  eyeCount: 4,
};

const handler1 = {
  get: function (target, prop, receiver) {
    if (prop === "secret") {
      return `${target.secret.substring(0, 4)} ... shhhh!`;
    }
    return Reflect.get(...arguments);
  },
};

const proxy1 = new Proxy(monster1, handler1);

console.log(proxy1.eyeCount);
// Expected output: 4

console.log(proxy1.secret);
// Expected output: "easi ... shhhh!"
구문
new Proxy(target, {
  get(target, property, receiver) {},
});
매개 변수

다음 매개변수는 get() 메서드에 전달됩니다. this는 처리기에 바인딩됩니다.

target

대상 객체

property

가져올 속성의 이름 또는 Symbol입니다.

receiver

프록시 또는 프록시에서 상속되는 객체입니다.

반환 값

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

설명

handler.get() 메서드는 속성 값을 가져오기 위한 트랩입니다.

가로채기

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

불변 조건

다음 불변량이 위반되면 프록시에서 TypeError가 발생합니다.

예제 속성 값을 얻기 위한 트랩

다음 코드는 속성 값을 가져오는 것을 트랩합니다.

const p = new Proxy(
  {},
  {
    get(target, property, receiver) {
      console.log(`called: ${property}`);
      return 10;
    },
  },
);

console.log(p.a); // "called: a"
// 10

다음 코드는 불변 조건을 위반합니다.

const obj = {};
Object.defineProperty(obj, "a", {
  configurable: false,
  enumerable: false,
  value: 10,
  writable: false,
});

const p = new Proxy(obj, {
  get(target, property) {
    return 20;
  },
});

p.a; // TypeError is thrown
명세서 브라우저 호환성 같이 보기

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