So why can we delete the properties of an object:

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

But you cannot delete a variable:

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

Nor can you delete a function:

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

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

Each attribute has zero or more attributes such as * readonly, dontenum, dontdelete, and internal **. You can think of them as labels-an attribute may or may not have a special internal attribute. In today's discussion,We are interested in dontdelete.

When declaring variables and functions,They become attributes of a variable object—either an activation object (in function code) or a global object (in global code) —that are accompanied by the internal attribute dontdelete. However, any explicitly/implicitly assigned property does not generate a dontdelete. And this is why we can delete some attributes but not others.

var global_object=this;
/* "foo" is a property of the global object,It is generated by variable declaration,So it has the internal attribute dontdelete
That is why it cannot be deleted * /
var foo=1;
delete foo;//false
typeof foo;//"number"
/* "bar

"Is a property of the global object,It is generated by variable declaration,So dontdelete child

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

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

delete global_object.baz;//true
typeof global_object.baz;//"undefined"

1.5, built-ins and dontdelete | build-ins and dontdelete

So that's why it all happened:A special internal property of the property controls whether the property can be deleted. Note:some properties of the built-in object have the internal property dontdelete, so they cannot be deleted Special arguments variables (as we know,The properties of the activation object) have dontdelete;the length (return parameter length) property of any function instance also has dontdelete:

(function () {
  //Cannot delete "arguments" because there is dontdelete
  delete arguments;//false;
  typeof arguments;//"object"
  //Can't delete the length of the function, because there is dontdelete
  function f () {};
  delete f.length;//false;
  typeof f.length;//"number"
}) ();

The attributes associated with the function arguments also have dontdelete and cannot be deleted as well.

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

1.6 Unassigned variable assignments | undeclared assignments

You may remember,Undeclared variable assignments become properties of global objects,Unless this attribute is found elsewhere in the scope chain. And now we understand the difference between attribute assignment and variable declaration-the latter generates dontdelete and the former does not-this is why undeclared variable assignments can be deleted.

var global_object=this;
/* Generate global object properties through variable declarations,Own dontdelete * /
var foo=1;
/* Generate global object properties through undeclared variable assignments,No dontdelete * /
delete foo;//false
delete bar;//true

Note:the internal attributes are determined when the attributes are generated,Subsequent assignments do not change the internal attributes of existing attributes. It is important to understand this distinction.

/* Dontdelete is generated when "foo" is created * /
function foo () {};
The assignment process after/* does not change the internal attributes of the existing attributes,dontdelete still exists * /
delete foo;//false;
typeof foo;//"number"
/* But when assigning a non-existing property,Created a property with no internal properties,So no dontdelete * /
delete bar;//true;
typeof bar;//"undefined"

to sum up:

Variable and function declarations are properties of the activation global object.

Attributes have internal attributes,One of them-dontdelete is responsible for determining whether an attribute can be deleted.

Variables and function declarations in global or function code generate properties that have dontdelete.

Function parameters are also attributes of the activation object,Also owns dontdelete.

Delete an attribute in an object:delete object.member

Can only delete own members

Only global variables declared by var do not let delete

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

ps:delete operator in javascript

Delete is one of the less frequently used operations in the JavaScript language.But sometimes,When we need to delete or empty,You need to delete it. In this article,We will dive into how to use it,And how it works.

The purpose of the deletion,As you think,Is to delete something,More specifically,It deletes the attributes of the object,For example:

var benjamin={
  "name":"zuojj",  "url":"http://www.zuojj.com"
delete benjamin.name;
//outputs:object {url:"http://www.zuojj.com"}
console.log (benjamin);

The delete operator will not delete ordinary variables,For example:

var benjamin="http://www.zuojj.com";
delete benjamin;
console.log (benjamin);

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

//because var isn "t used, this is a property of window
delete window.benjamin;
//referenceerror:benjamin is not defined
console.log (benjamin);

The delete operator also has a return value,If deleting an attribute succeeds,Returns true if the attribute cannot be deleted,Because the attribute is not writable,Will return false, or an error will be thrown if in strict mode.

var benjamin={
  "name":"zuojj",  "url":"http://www.zuojj.com"
var namedeleted=delete benjamin.name;
console.log (namedeleted);
"use strict";
var benjamin_="zuojj";
//outputs:uncaught syntaxerror:delete of an unqualified identifier in strict mode.
delete benjamin_;

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

sometimes,JavaScript development is not about deleting an attribute,Instead, set the value of this property to null. Like this:

var benjamin={
  "name":"zuojj",  "url":"http://www.zuojj.com"

Although this effectively cuts off the attribute from the original value,But the attribute itself still exists on the object,You can see the following:

//outputs:object {name:null, url:"http://www.zuojj.com"}
console.log (benjamin);

Also, loop operations like in and for in will not report the existence of null attributesIf you use an object,May use these methods to inspect an object,You might want to make sure you really remove any unwanted attributes.

Finally, you should remember,Deletion does not destroy the value of the attribute,Just the attribute itself,Look at the following example:

var name="zuojj",    benjamin=();
delete benjamin.name;
console.log (name);

Here, name and benjamin.name are mapped to the same value,Really as you see,Deleting benjamin.name does not affect name.

  • Previous Complete example of consistent hash algorithm implemented in PHP
  • Next A complete instance of the PHP wrapper class implementing PHP redis