This article is from MDN official website

A few days ago I read an article about Delete, which mentioned the relationship and difference between the Delete operation and the Delete operation of map, which greatly aroused my interest in the Delete operator, which is very common in JS.

I was intrigued by the fact that the author of that article mentioned that map’s DELETE operation is better than the delete operator’s ability to delete objects directly, and I’ll show you what I’ve seen and understood.

delete

The delete operator is used to delete an attribute of an object. If there is no reference to this property, it will eventually be released.

const Employee = {
  firstname: 'John'.lastname: 'Doe'
};

console.log(Employee.firstname);
// expected output: "John"

delete Employee.firstname;

console.log(Employee.firstname);
// expected output: undefined
Copy the code

This is a simple use of the delete operator: delete expression.

The delete operation also returns a result: true indicates that the deletion succeeded, false indicates that the deletion failed. Note that deleting a nonexistent property also returns true.

In terms of memory management: The operation of DELETE does not directly affect the memory and has nothing to do with the direct memory release. The function of delete here is to disconnect the reference of the object. After the disconnect, when the memory reclamation is triggered, the object will be reclaimed (this involves the knowledge of memory management, and will not be discussed here temporarily).


Unsettable property

When a property is set to unsettable, delete has no effect and returns false.

var Employee = {};
Object.defineProperty(Employee, 'name', {configurable: false});
console.log(delete Employee.name);  // returns false
Copy the code

Non-settable attributes created by var, let, and const cannot be deleted by delete.

var nameOther = 'XYZ';

// Get the global attribute using the following method:
Object.getOwnPropertyDescriptor(window.'nameOther');

Object {value: "XYZ",
// writable: true,
// enumerable: true,
// configurable: false}

// Because "nameOther" is added using the var keyword,
// It is configured with a non-configurable system.
delete nameOther;   // return false
Copy the code

In strict mode, such an operation throws an exception.

Any variable declared using var is marked as unsettable.

Reflect.deleteProperty()

It is similar to the delete operator, but is a function. This method has a return value of Boolean type. The demo is as follows.

var obj = { x: 1.y: 2 };
Reflect.deleteProperty(obj, "x"); // true
obj; // { y: 2 }

var arr = [1.2.3.4.5];
Reflect.deleteProperty(arr, "3"); // true
arr; // [1, 2, 3, 5]

// If the attribute does not exist, return true
Reflect.deleteProperty({}, "foo"); // true

// If the property is not configurable, return false
Reflect.deleteProperty(Object.freeze({foo: 1}), "foo"); // false
Copy the code

Map.prototype.delete()

The delete() method removes the specified element from the Map object.

Return value: If the element is present in the Map object, remove it and return true; Otherwise return false if the element does not exist.

const map1 = new Map(a); map1.set('bar'.'foo');

console.log(map1.delete('bar'));
// expected result: true
// (true indicates successful removal)

console.log(map1.has('bar'));
// expected result: false
Copy the code