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

handler.has() - JavaScript | MDN

handler.has()

Baseline Widely available

handler.has() 메서드는 in 연산자에 대한 트랩입니다.

시도해 보기
const handler1 = {
  has(target, key) {
    if (key[0] === "_") {
      return false;
    }
    return key in target;
  },
};

const monster1 = {
  _secret: "easily scared",
  eyeCount: 4,
};

const proxy1 = new Proxy(monster1, handler1);
console.log("eyeCount" in proxy1);
// Expected output: true

console.log("_secret" in proxy1);
// Expected output: false

console.log("_secret" in monster1);
// Expected output: true
구문
new Proxy(target, {
  has(target, prop) {},
});
매개 변수

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

target

대상 객체

prop

존재 여부를 확인할 속성의 이름 또는 Symbol

반환 값

has() 메서드는 불리언 값을 반환합니다.

설명

handler.has() 메서드는 in 연산자에 대한 트랩입니다.

가로채기

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

불변 조건

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

예제 in 연산자 트랩

다음 코드는 in 연산자를 트랩합니다.

const p = new Proxy(
  {},
  {
    has(target, prop) {
      console.log(`called: ${prop}`);
      return true;
    },
  },
);

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

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

const obj = { a: 10 };
Object.preventExtensions(obj);

const p = new Proxy(obj, {
  has(target, prop) {
    return false;
  },
});

"a" in p; // 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