handler.ownKeys()
handler.ownKeys()
메서드는 Reflect.ownKeys()
에 대한 트랩입니다.
시도해보기
구문
new Proxy(target, {
ownKeys(target) {
}
});
매개 변수
다음 매개변수는 ownKeys()
메서드에 전달됩니다. this
는 처리기에 바인딩됩니다.
target
-
대상 객체
반환 값
ownKeys()
메서드는 열거 가능한 객체를 반환합니다.
설명
handler.ownKeys()
메서드는 Reflect.ownKeys()
에 대한 트랩입니다.
가로채기
이 트랩은 다음 작업을 가로챌 수 있습니다.
불변 조건
예제
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
명세서
Specification |
---|
ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys |
브라우저 호환성
BCD tables only load in the browser