The delete and delete operators in javascript

  • 2020-11-03 22:01:55
  • OfStack

So, why can we delete an object's property:


var x = { a: 1 };
delete x.a; // true
x.a; // undefined

But you cannot delete a variable:


var x = 1;
delete x; // false;
x; // 1

You cannot delete a function:


function x() {};
delete x; // false;
typeof x; // "function"

Note: delete returns false only if 1 attribute cannot be deleted.

Each attribute has zero and at most internal attributes -- *ReadOnly, DontEnum, DontDelete, and Internal**. You can think of them as tags -- a property may or may not have a particular internal property. In today's discussion, we are interested in DontDelete.

When variables and functions are declared, they become properties of the variable object (Variable object) -- either the activation object (in the function code) or the global object (in the global code) -- that accompany the generation of the internal property DontDelete. However, any explicitly/implicitly assigned property does not generate DontDelete. And that's essentially why we can remove 1 of the attributes and not the others.


var GLOBAL_OBJECT = this;

/* 'foo' is a property of the global object that is generated by variable declarations and therefore has the internal property DontDelete

That's why it can't be deleted */


var foo = 1;
delete foo; // false
typeof foo; // "number"
/* 'bar

'is a property of the global object that is generated by a variable declaration and therefore has DontDelete children

That's why it can't be deleted */


function bar() {};
delete bar; // false
typeof bar; // "function"

/* 'baz' is also a property of the global object,

However, it is generated by attribute assignment, so there is no DontDelete

That's why it can be deleted */


GLOBAL_OBJECT.baz = "baz";
delete GLOBAL_OBJECT.baz; // true
typeof GLOBAL_OBJECT.baz; // "undefined"

1.5 built in and DontDelete | Build-ins and DontDelete

So that's why all of this cutting happens: a special internal property of the property controls whether or not the property can be deleted. Note: 1 some properties of the built-in object have the internal property DontDelete and therefore cannot be deleted. The special arguments variable (as we know, the properties of the activated object) has DontDelete; The length (return parameter length) attribute of any function instance also has DontDelete:


(function() {
  // Can't delete 'arguments' Because there are DontDelete
  delete arguments; // false;
  typeof arguments; // "object"

  // You can't delete a function length, Because there are DontDelete
  function f() {};
  delete f.length; // false;
  typeof f.length; // "number"
}) ();

The attribute associated with the function arguments also has DontDelete and cannot be deleted again


(function(foo,bar) {
  delete foo; // false
  foo; // 1

  delete bar; // false
  bar; // "bah"
}) (1,"bah");

1.6. Undeclared variables are assigned | Undeclared assignments

As you may recall, undeclared variable assignments become properties of global objects unless the 1 property is found elsewhere in the scope chain. Now that we know the difference between attribute assignments and variable declarations -- the latter generates DontDelete and the former does not -- this is why undeclared variable assignments can be deleted.


var GLOBAL_OBJECT = this;

/*  Properties of a global object are generated by a variable declaration DontDelete */
var foo = 1;

/*  Properties that generate global objects from undeclared variable assignments, none DontDelete */
bar = 2;
delete foo; // false
delete bar; // true

Note: Internal properties are determined at the time of property generation, and subsequent assignments do not change the internal properties of existing properties. It is important to understand this distinction.


var x = 1;
delete x; // false;
x; // 1
0

Conclusion:

Variables and function declarations are properties of the activation (Activation) global (Global) object.

Attributes have internal attributes, of which 1 -- DontDelete is responsible for determining whether a property can be deleted.

Variables, function declarations in global code or function code generate properties that have DontDelete.

The function arguments, which are also properties of the activation object, also have DontDelete.

Delete the properties in the object: delete object. Members of the

Only owned members can be deleted

Only global variables declared by var do not allow delete

Global members added using window. Or window[""] can be delete

ps: The delete operator in Javascript

Delete is one of the less frequently used operations in the Javascript language, but sometimes the delete operation is needed when we need to do delete or empty the action. In this article, we'll take a closer look at how to use it and how it works.

The purpose of deletion, as you might expect, is to remove something, or more specifically, to remove an object's properties, as in the following example:


var x = 1;
delete x; // false;
x; // 1
1

The delete operator will not delete normal variables, as shown in the following example:


var x = 1;
delete x; // false;
x; // 1
2

However, it can remove "global variables" because they are, in fact, properties of the global object (window in the browser) object.


// Because var isn't used, this is a property of window
benjamin = "zuojj";
delete window.benjamin;
// ReferenceError: benjamin is not defined
console.log(benjamin);

The delete operator also has a return value, returning true if the deletion of a property is successful, false if the property cannot be deleted because it is not writable, or throwing an error if in strict mode.


var x = 1;
delete x; // false;
x; // 1
4

You may not know when to use the delete operator. The answer is, as long as you really want to remove one property from the object.

Sometimes, instead of deleting an attribute, Javascript development sets the attribute value to null. Look like this:


var x = 1;
delete x; // false;
x; // 1
5

Although this effectively cuts off the property from the original value, the property itself still exists on the object, as you can see below:


var x = 1;
delete x; // false;
x; // 1
6

Also, loops like in and for in will not report the presence of the null attribute. If you use an object, you may use these methods to check an object. You may want to make sure that you actually delete any attributes that you don't need.

Finally, you should remember that deleting does not break the value of the property, just the property itself. See the following example:


var x = 1;
delete x; // false;
x; // 1
7

Here, name and ES174en.name map to the same value, and as you can see, deleting benjamin.name does not affect name.

So, that's my overview of the delete operator, and I welcome your comments and criticisms.


Related articles: