Join MDN and developers like you at Mozilla's View Source conference, November 2-4 in Portland, Oregon. Learn more at https://viewsourceconf.org/.

handler.has()

공헌자 숫자: 4명
아직 자원 봉사자들이 한국어로 현재 문서를 번역하지 않았습니다. 가입해서 이 문서가 번역되는 일에 함께 해 주세요!

This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

The handler.has() method is a trap for the in operator.

Syntax

var p = new Proxy(target, {
  has: function(target, prop) {
  }
});

Parameters

The following parameters are passed to has method. this is bound to the handler.

target
The target object.
prop
The name of the property to check for existence.

Return value

The has method must return a boolean value.

Description

The handler.has method is a trap for the in operator.

Interceptions

This trap can intercept these operations:

  • Property query: foo in proxy
  • Inherited property query: foo in Object.create(proxy)
  • with check: with(proxy) { (foo); }
  • Reflect.has()

Invariants

If the following invariants are violated, the proxy will throw a TypeError:

  • A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.
  • A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.

Examples

The following code traps the in operator.

var p = new Proxy({}, {
  has: function(target, prop) {
    console.log("called: " + prop);
    return true;
  }
});

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

The following code violates an invariant.

var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
  has: function(target, prop) {
    return false;
  }
});

"a" in p; // TypeError is thrown

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of '[[HasProperty]]' in that specification.
Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? 18 (18) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? 18.0 (18) ? ? ?

See also

문서 태그 및 공헌자

Contributors to this page: fscholz, leobalter, getify, arai
최종 변경: fscholz,