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

handler.ownKeys() - JavaScript | MDN

handler.ownKeys()

Baseline Widely available

handler.ownKeys() 메서드는 Reflect.ownKeys()에 대한 트랩입니다.

시도해 보기
const monster1 = {
  _age: 111,
  [Symbol("secret")]: "I am scared!",
  eyeCount: 4,
};

const handler1 = {
  ownKeys(target) {
    return Reflect.ownKeys(target);
  },
};

const proxy1 = new Proxy(monster1, handler1);

for (const key of Object.keys(proxy1)) {
  console.log(key);
  // Expected output: "_age"
  // Expected output: "eyeCount"
}
구문
new Proxy(target, {
  ownKeys(target) {},
});
매개 변수

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

target

대상 객체

반환 값

ownKeys() 메서드는 열거 가능한 객체를 반환합니다.

설명

handler.ownKeys() 메서드는 Reflect.ownKeys()에 대한 트랩입니다.

가로채기 불변 조건

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

예제 getOwnPropertyNames 트랩

다음 코드는 Object.getOwnPropertyNames()를 트랩합니다.

const p = new Proxy(
  {},
  {
    ownKeys(target) {
      console.log("called");
      return ["a", "b", "c"];
    },
  },
);

console.log(Object.getOwnPropertyNames(p)); // "called"
// [ 'a', 'b', 'c' ]

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

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

const p = new Proxy(obj, {
  ownKeys(target) {
    return [123, 12.5, true, false, undefined, null, {}, []];
  },
});

console.log(Object.getOwnPropertyNames(p));

// TypeError: proxy [[OwnPropertyKeys]] must return an array
// with only string and symbol elements
명세서 브라우저 호환성 같이 보기

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