A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get below:

handler.get() - JavaScript | MDN

handler.get()

Baseline Widely available

The handler.get() method is a trap for the [[Get]] object internal method, which is used by operations such as property accessors.

Try it
const monster1 = {
  secret: "easily scared",
  eyeCount: 4,
};

const handler1 = {
  get(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!"
Syntax
new Proxy(target, {
  get(target, property, receiver) {
  }
})
Parameters

The following parameters are passed to the get() method. this is bound to the handler.

target

The target object.

property

A string or Symbol representing the property name.

receiver

The this value for getters; see Reflect.get(). This is usually either the proxy itself or an object that inherits from the proxy.

Return value

The get() method can return any value, representing the property value.

Description Interceptions

This trap can intercept these operations:

Or any other operation that invokes the [[Get]] internal method.

Invariants

The proxy's [[Get]] internal method throws a TypeError if the handler definition violates one of the following invariants:

Examples Trap for getting a property value

The following code traps getting a property value.

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

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

The following code violates an invariant.

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
Specifications Browser compatibility See also

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