The delete profile

The MDN: delete operator is used to delete an attribute of an object. If there is no reference to this property, it will eventually be released. This simple explanation raises the following questions:

What does delete have to do with memory free?

The delete operator is used to delete properties from objects. When you use the delete operator, it does not immediately release the reference to the property. It simply unbinds the property from the object, as we can see in the following code:

var obj = {
    a: 1,
    b: { c: 1}
}
var new_obj = obj.b
delete obj.a
delete obj.b
console.log(obj) // {}
console.log(new_obj) // {c: 1}
Copy the code

New_obj refers to the b attribute of obj. When we remove the A and B attributes of obj, obj becomes an empty object, and the reference to the B attribute of obj still exists. So it’s not recycled; As you can see from this example, the delete operator has nothing to do with freeing memory; it simply disconnects the object from the reference to the property. The block of memory in which the property is located will be freed by the next garbage collection only if there are no other variables referencing the property you deleted.

Can DELETE only be used on object properties? What happens if I apply it to values?

The syntax of the delete operator is:

delete expression
Copy the code

That is, the DELETE operator treats its operand as an expression and deletes the result of the expression’s evaluation. After all, “delete x” deletes the Result of an expression, not the x expression, or the Value of that deleted expression. So the x here is not a Value, but the Result of an expression operation. JavaScript needs to check the Result type before further deleting:

  • If it is a value, the traditional JavaScript convention returns true;
  • If it is a reference, the reference is parsed to determine what to do. This procedure illustrates the ECMAScript convention that the Result of any expression evaluated is either a value or a reference. And it’s important to note that in this description, objects are also values. Non-reference types, to be exact. Such as:
    delete {}It’s also treated as a value. When deleting these so-called values, the rule is that the delete operation returns true when it finds that its operand is of value/non-reference type. Nothing really happened. There is an exception here, and we’ll explain why; is
    delete undefinedThe result returned is false, which contradicts our previous principle. Which brings us to the next question: What can’t delete by DELETE?

What can’t delete remove?

The delete allows you to remove any non-configurable property, which works without any additional control system. The delete works without any additional control system. The delete works without any additional control system.

  • The variable declared by var is directly attached to the global object as a property of the global object. When mounted, the variable can be configured as a non-configurable property

    var a = 1; B = 2 / / use not directly declare variables in a strict mode won't be an error, and can also be mounted to the console under the global Object. The log (Object. GetOwnPropertyDescriptor (window,"a"))
       // {value: 1, writable: true, enumerable: true, configurable: false}
       console.log(Object.getOwnPropertyDescriptor(window, "b"))
       // {value: 2, writable: true, enumerable: true, configurable: true}
       delete a // false 
       delete b // true
    Copy the code

  • The variable declared by let and const can be deleted without any additional information. The variable declared by let and const can be mounted under the specified scope, and is non-configurable. Add sample code if you think of examples)

Why does delete undefined return false

Here is the great God’s reply:

In early JavaScript, undefined was a special value that was “evaluated” at runtime by a void operation, a function that did not return a value, a variable that was declared but not assigned a value, and so on. So in earlier versions of JavaScript, you didn’t have a way to tell “undefined” directly. For example, you couldn’t write x === undefined, and you could only write something like typeof(x) ===’undefined’. Later (and not long after), the specification agreed to use undefined as a name that could be accessed by default, similar to NULL. This creates a paradox because undefined is so important that most early frameworks or engines declared it as a “global name”. That is, ECMAScript currently has no way to normalize it as a keyword, handle it as a reserved word, etc., which looks like NULL, but there is no way to enforce it at the specification level. So… ECMAScript does a trick: how about we declare undefined as a global property? ! Yeah, yeah, good. So you see, undefined looks like null on the current engine, and in the ECMAScript specification they’re still flat (raw), and they’re very close to each other, and in the end they’re all concepts from the original JavaScript 1.x. But undefined/null is implemented differently: undefined is a global attribute and NULL is a keyword. Because undefined is a global attribute, delete undefined is actually delete global. Undefined removes references, not values. This property is read-only, so it returns false.

The history of reasoning is interesting, as evidenced by the following code:

console.log(void 0 === undefined) // true
delete void 0 // true
delete undefined // false
delete null // true
console.log(Object.getOwnPropertyDescriptor(window, 'undefined'))
// {value: undefined, writable: false, enumerable: false, configurable: false}
console.log(Object.getOwnPropertyDescriptor(window, 'null'))
// undefined
Copy the code

Delete:

  • Delete only returns false if the object property fails to be removed, which happens only if the property is a non-configurable property.
  • Delete usually returns true if the operation did nothing (although the first one also did nothing), or if the object property was successfully removed
  • The delete operation only works on its own property. If the object has a property in its stereotype chain with the same name as the property to be deleted, the object will use that property in the stereotype chain after deleting the property

Reference:

  1. MDN: delete operator
  2. Geek Time: the Core principles of JavaScript

This article is formatted using MDNICE