Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Latest commit

 

History

History
109 lines (77 loc) · 3.3 KB

deleteproperty.md

File metadata and controls

109 lines (77 loc) · 3.3 KB

Observer.deleteProperty()

This method is used to delete an object's property. It corresponds to the JavaScript's Reflect.deleteProperty() function, which is itself the programmatic alternative to the assignment expression – delete obj.property.

Observer.deleteProperty() brings the added benefit of triggering observers and interceptors.

The Observer.del() function is an alias of this function and could be used interchangeably.

Syntax

// Delete a specific property
Observer.deleteProperty(obj, propertyName);

// Delete a list of properties
Observer.deleteProperty(obj, propertyNames);

Parameters

  • obj - an object or array.
  • propertyNames/propertyName - the list of properties, or a property, to delete.

Return Value

Boolean

Usage

ADeleting a Specific Property

// On an object
Observer.deleteProperty(obj, 'fruit');
// On an array
Observer.deleteProperty(arr, 0);

Deleting a List of Properties

// On an object
Observer.deleteProperty(obj, ['fruit', 'brand']);
// On an array
Observer.deleteProperty(arr, [0, 3]);

Usage as a Trap's "deleteProperty" Handler

Observer.deleteProperty() can be used as the "deleteProperty" handler in Proxy traps.

let _obj = new Proxy(obj, {deleteProperty: Observer.deleteProperty});
let _arr = new Proxy(arr, {deleteProperty: Observer.deleteProperty});

Delete operations will now be forwarded to Observer.deleteProperty() and observers and interceptors that may be bound to the object will continue to respond.

delete _obj.fruit;
delete _arr[2];

Intercepting Observer.deleteProperty()

Using Observer.intercept(), it is possible to intercept calls to Observer.deleteProperty(). When a "del" operation triggers an interceptor, the interceptor will receive an event object containing the property name to delete.

Observer.intercept(obj, 'del', (event, recieved, next) => {
    // What we recieved...
    console.log(event.name);
    // The delete operation
    delete obj[event.name];
    // The return value - Boolean
    return true;
});

Observer.deleteProperty(obj, 'fruit');

When the "del" operation is of multiple deletion, the interceptor gets fired for each pair while also recieving the total list of properties as a hint - via event.related.

Observer.intercept(obj, 'del', (event, recieved, next) => {
    // What we recieved...
    console.log(event.name, event.related);
    // The delete operation
    delete obj[event.name];
    // The return value - Boolean
    return true;
});

Observer.deleteProperty(obj, ['orange', 'apple']);

The above should trigger our interceptor twice with event.related being ['fruit', 'brand'].

The interceptor is expected to return true when the deletion is successful; false otherwise.

Related Methods