recursal/MDN huggingface dataset:
handler.preventExtensions()
===========================

The **`handler.preventExtensions()`** method is a trap for the `[[PreventExtensions]]` object internal method, which is used by operations such as `Object.preventExtensions()`.

Try it
------

Syntax
------


```js
new Proxy(target, {
  preventExtensions(target) {
  }
});
```

### Parameters

The following parameter is passed to the `preventExtensions()` method. `this` is bound to the handler.

`target`

The target object.

### Return value

The `preventExtensions()` method must return a boolean value.

Description
-----------

### Interceptions

This trap can intercept these operations:

* `Object.preventExtensions()`
* `Reflect.preventExtensions()`
* `Object.seal()`
* `Object.freeze()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* `Object.preventExtensions(proxy)` only returns `true` if `Object.isExtensible(proxy)` is `false`.
Examples
--------

### Trapping of preventExtensions

The following code traps `Object.preventExtensions()`.


```js
const p = new Proxy(
  {},
  {
    preventExtensions(target) {
      console.log("called");
      Object.preventExtensions(target);
      return true;
    },
  },
);

console.log(Object.preventExtensions(p));
// "called"
// false
```


The following code violates the invariant.


```js
const p = new Proxy(
  {},
  {
    preventExtensions(target) {
      return true;
    },
  },
);

Object.preventExtensions(p); // TypeError is thrown
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-preventextensions |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Object.preventExtensions()`
* `Reflect.preventExtensions()`
handler.defineProperty()
========================

The **`handler.defineProperty()`** method is a trap for the `[[DefineOwnProperty]]` object internal method, which is used by operations such as `Object.defineProperty()`.

Try it
------

Syntax
------


```js
new Proxy(target, {
  defineProperty(target, property, descriptor) {
  }
});
```

### Parameters

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

`target`

The target object.

`property`

 The name or `Symbol` of the property whose description is to be
 retrieved.


`descriptor`

The descriptor for the property being defined or modified.

### Return value

 The `defineProperty()` method must return a `Boolean` indicating
 whether or not the property has been successfully defined.

Description
-----------

### Interceptions

This trap can intercept these operations:

* `Object.defineProperty()`, `Object.defineProperties()`
* `Reflect.defineProperty()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* A property cannot be added, if the target object is not extensible.
* A property cannot be added as or modified to be non-configurable, if it does not
 exists as a non-configurable own property of the target object.
* A property may not be non-configurable, if a corresponding configurable property of
 the target object exists.
* If a property has a corresponding target object property then
 `Object.defineProperty(target, prop, descriptor)`
 will not throw an exception.
* In strict mode, a `false` return value from the
 `defineProperty()` handler will throw a `TypeError` exception.
Examples
--------

### Trapping of defineProperty

The following code traps `Object.defineProperty()`.


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

const desc = { configurable: true, enumerable: true, value: 10 };
Object.defineProperty(p, "a", desc); // "called: a"
```


 When calling `Object.defineProperty()` or
 `Reflect.defineProperty()`, the `descriptor` passed to
 `defineProperty()` trap has one restriction—only following properties are
 usable (non-standard properties will be ignored):

* `enumerable`
* `configurable`
* `writable`
* `value`
* `get`
* `set`


```js
const p = new Proxy(
  {},
  {
    defineProperty(target, prop, descriptor) {
      console.log(descriptor);
      return Reflect.defineProperty(target, prop, descriptor);
    },
  },
);

Object.defineProperty(p, "name", {
  value: "proxy",
  type: "custom",
}); // { value: 'proxy' }
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Object.defineProperty()`
* `Reflect.defineProperty()`
handler.get()
=============

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

Try it
------

Syntax
------


```js
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`

The name or `Symbol` of the property to get.

`receiver`

Either the proxy or an object that inherits from the proxy.

### Return value

The `get()` method can return any value.

Description
-----------

### Interceptions

This trap can intercept these operations:

* Property access: `proxy[foo]` and `proxy.bar`
* `Reflect.get()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* The value reported for a property must be the same as the value of the corresponding
 target object property if the target object property is a non-writable,
 non-configurable own data property.
* The value reported for a property must be undefined if the corresponding target
 object property is a non-configurable own accessor property that has
 `undefined` as its `[[Get]]` attribute.
Examples
--------

### Trap for getting a property value

The following code traps getting a property value.


```js
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.


```js
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
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Reflect.get()`
handler.isExtensible()
======================

The **`handler.isExtensible()`** method is a trap for the `[[IsExtensible]]` object internal method, which is used by operations such as `Object.isExtensible()`.

Try it
------

Syntax
------


```js
new Proxy(target, {
  isExtensible(target) {
  }
});
```

### Parameters

 The following parameter is passed to the `isExtensible()` method.
 `this` is bound to the handler.

`target`

The target object.

### Return value

The `isExtensible()` method must return a boolean value.

Description
-----------

### Interceptions

This trap can intercept these operations:

* `Object.isExtensible()`
* `Reflect.isExtensible()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* `Object.isExtensible(proxy)` must return the same value as
 `Object.isExtensible(target)`.
Examples
--------

### Trapping of isExtensible

The following code traps `Object.isExtensible()`.


```js
const p = new Proxy(
  {},
  {
    isExtensible(target) {
      console.log("called");
      return true;
    },
  },
);

console.log(Object.isExtensible(p));
// "called"
// true
```


The following code violates the invariant.


```js
const p = new Proxy(
  {},
  {
    isExtensible(target) {
      return false;
    },
  },
);

Object.isExtensible(p); // TypeError is thrown
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-isextensible |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Object.isExtensible()`
* `Reflect.isExtensible()`
* `Reflect.preventExtensions()`
handler.ownKeys()
=================

The **`handler.ownKeys()`** method is a trap for the `[[OwnPropertyKeys]]` object internal method, which is used by operations such as `Object.keys()`, `Reflect.ownKeys()`, etc.

Try it
------

Syntax
------


```js
new Proxy(target, {
  ownKeys(target) {
  }
});
```

### Parameters

 The following parameter is passed to the `ownKeys()` method.
 `this` is bound to the handler.

`target`

The target object.

### Return value

The `ownKeys()` method must return an enumerable object.

Description
-----------

### Interceptions

This trap can intercept these operations:

* `Object.getOwnPropertyNames()`
* `Object.getOwnPropertySymbols()`
* `Object.keys()`
* `Reflect.ownKeys()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* The result of `ownKeys()` must be an array.
* The type of each array element is either a `String` or a
 `Symbol`.
* The result List must contain the keys of all non-configurable own properties of the
 target object.
* If the target object is not extensible, then the result List must contain all the
 keys of the own properties of the target object and no other values.
Examples
--------

### Trapping of getOwnPropertyNames

The following code traps `Object.getOwnPropertyNames()`.


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

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


The following code violates an invariant.


```js
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
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Object.getOwnPropertyNames()`
* `Reflect.ownKeys()`
handler.setPrototypeOf()
========================

The **`handler.setPrototypeOf()`** method is a trap for the `[[SetPrototypeOf]]` object internal method, which is used by operations such as `Object.setPrototypeOf()`.

Try it
------

Syntax
------


```js
new Proxy(target, {
  setPrototypeOf(target, prototype) {
  }
});
```

### Parameters

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

`target`

The target object.

`prototype`

The object's new prototype or `null`.

### Return value

 The `setPrototypeOf()` method returns `true` if the
 `[[Prototype]]` was successfully changed, otherwise `false`.

Description
-----------

### Interceptions

This trap can intercept these operations:

* `Object.setPrototypeOf()`
* `Reflect.setPrototypeOf()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* If `target` is not extensible, the `prototype`
 parameter must be the same value as
 `Object.getPrototypeOf(target)`.
Examples
--------

 If you want to disallow setting a new prototype for your object, your handler's
 `setPrototypeOf()` method can either return `false`, or it can
 throw an exception.

### Approach 1: Returning false

 This approach means that any mutating operation that throws an exception on failure to
 mutate, must create the exception itself.

 For example, `Object.setPrototypeOf()` will create and throw a
 `TypeError` itself. If the mutation is performed by an operation that
 *doesn't* ordinarily throw in case of failure, such as
 `Reflect.setPrototypeOf()`, no exception will be thrown.


```js
const handlerReturnsFalse = {
  setPrototypeOf(target, newProto) {
    return false;
  },
};

const newProto = {},
  target = {};

const p1 = new Proxy(target, handlerReturnsFalse);
Object.setPrototypeOf(p1, newProto); // throws a TypeError
Reflect.setPrototypeOf(p1, newProto); // returns false
```

### Approach 2: Throwing an Exception

 The latter approach will cause *any* operation that attempts to mutate, to
 throw. This approach is best if you want even non-throwing operations to throw on
 failure, or you want to throw a custom exception value.


```js
const handlerThrows = {
  setPrototypeOf(target, newProto) {
    throw new Error("custom error");
  },
};

const newProto = {},
  target = {};

const p2 = new Proxy(target, handlerThrows);
Object.setPrototypeOf(p2, newProto); // throws new Error("custom error")
Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Object.setPrototypeOf()`
* `Reflect.setPrototypeOf()`
handler.has()
=============

The **`handler.has()`** method is a trap for the `[[HasProperty]]` object internal method, which is used by operations such as the `in` operator.

Try it
------

Syntax
------


```js
new Proxy(target, {
  has(target, prop) {
  }
});
```

### Parameters

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

`target`

The target object.

`prop`

The name or `Symbol` of the property to check for existence.

### Return value

The `has()` method must return a boolean value.

Description
-----------

### Interceptions

This trap can intercept these operations:

* The `in` operator: `foo in proxy`
* `with` check: `with(proxy) { (foo); }`
* `Reflect.has()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* 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
--------

### Trapping the in operator

The following code traps the `in` operator.


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

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


The following code violates an invariant.


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

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

"a" in p; // TypeError is thrown
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `in`
* `Reflect.has()`
handler.apply()
===============

The **`handler.apply()`** method is a trap for the `[[Call]]` object internal method, which is used by operations such as function calls.

Try it
------

Syntax
------


```js
new Proxy(target, {
  apply(target, thisArg, argumentsList) {
  }
});
```

### Parameters

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

`target`

The target callable object.

`thisArg`

The `this` argument for the call.

`argumentsList`

The list of arguments for the call.

### Return value

The `apply()` method can return any value.

Description
-----------

### Interceptions

This trap can intercept these operations:

* Function call: `proxy(...args)`
* `Function.prototype.apply()` and `Function.prototype.call()`
* `Reflect.apply()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* The `target` must be a callable itself. That is, it must be a function object.
Examples
--------

### Trapping a function call

The following code traps a function call.


```js
const p = new Proxy(function () {}, {
  apply(target, thisArg, argumentsList) {
    console.log(`called: ${argumentsList}`);
    return argumentsList[0] + argumentsList[1] + argumentsList[2];
  },
});

console.log(p(1, 2, 3)); // "called: 1,2,3"
// 6
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `Function.prototype.apply()`
* `Function.prototype.call()`
* `Reflect.apply()`
handler.construct()
===================

The **`handler.construct()`** method is a trap for the `[[Construct]]` object internal method, which is used by operations such as the `new` operator. In order for the new operation to be valid on the resulting Proxy object, the target used to initialize the proxy must itself be a valid constructor.

Try it
------

Syntax
------


```js
new Proxy(target, {
  construct(target, argumentsList, newTarget) {
  }
});
```

### Parameters

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

`target`

The target object.

`argumentsList`

The list of arguments for the constructor.

`newTarget`

The constructor that was originally called.

### Return value

The `construct` method must return an object.

Description
-----------

### Interceptions

This trap can intercept these operations:

* The `new` operator: `new myFunction(...args)`
* `Reflect.construct()`

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

### Invariants

If the following invariants are violated, the trap throws a `TypeError` when invoked.

* The result must be an `Object`.
Examples
--------

### Trapping the new operator

The following code traps the `new` operator.


```js
const p = new Proxy(function () {}, {
  construct(target, argumentsList, newTarget) {
    console.log(`called: ${argumentsList}`);
    return { value: argumentsList[0] \* 10 };
  },
});

console.log(new p(1).value); // "called: 1"
// 10
```


The following code violates the invariant.


```js
const p = new Proxy(function () {}, {
  construct(target, argumentsList, newTarget) {
    return 1;
  },
});

new p(); // TypeError is thrown
```


The following code improperly initializes the proxy. The `target` in Proxy initialization must itself be a valid constructor for the `new` operator.


```js
const p = new Proxy(
  {},
  {
    construct(target, argumentsList, newTarget) {
      return {};
    },
  },
);

new p(); // TypeError is thrown, "p" is not a constructor
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `Proxy`
* `Proxy()` constructor
* `new`
* `Reflect.construct()`
WeakSet() constructor
=====================

The **`WeakSet()`** constructor creates `WeakSet` objects.

Syntax
------


```js
new WeakSet()
new WeakSet(iterable)
```


**Note:** `WeakSet()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.

### Parameters

`iterable` Optional

If an iterable object is passed, all of its elements will be added to the new `WeakSet`. `null` is treated as `undefined`.

Examples
--------

### Using the WeakSet object


```js
const ws = new WeakSet();
const foo = {};
const bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo); // true
ws.has(bar); // true

ws.delete(foo); // removes foo from the set
ws.has(foo); // false, foo has been removed
ws.has(bar); // true, bar is retained
```


Note that `foo !== bar`. While they are similar objects, *they are not
**the same object***. And so they are both added to the set.

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-weakset-constructor |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `WeakSet` in `core-js`
* `WeakSet`
WeakSet.prototype.delete()
==========================

The **`delete()`** method of `WeakSet` instances removes the specified element from this `WeakSet`.

Try it
------

Syntax
------


```js
weakSetInstance.delete(value)
```

### Parameters

`value`

The value to remove from the `WeakSet` object.

### Return value

`true` if an element in the `WeakSet` object has been removed successfully. `false` if the `value` is not found in the `WeakSet`. Always returns `false` if `value` is not an object or a non-registered symbol.

Examples
--------

### Using the delete() method


```js
const ws = new WeakSet();
const obj = {};

ws.add(window);

ws.delete(obj); // Returns false. No obj found to be deleted.
ws.delete(window); // Returns true. Successfully removed.

ws.has(window); // Returns false. The window is no longer present in the WeakSet.
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-weakset.prototype.delete |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* `WeakSet`
* `WeakSet.prototype.add()`
* `WeakSet.prototype.has()`