Delete mechanism 3 in JS

Source: Internet
Author: User
Tags getbase

Title: Explanation of the delete mechanism in JavaScript (3)
-Little su 2010-05-17 read: 17
-Comment: 0 | comment | returns comment

IE bugs

This chapter is only intended for bugs in IE!

In IE (at least IE6-IE8), the following expression throws an error (executed in Global Code ):

This. x = 1;
Delete X; // typeerror: Object doesn' t support this action


This is the same, but the exception is different, but it is more interesting:

VaR x = 1;
Delete this. X; // typeerror: cannot delete 'this. X'

In IE, it seems that the variables declared in the Global Code cannot create attributes in the global object. Create attributes by assigning values (this.x = 1), And thendeleteDeleting X will throw an error. Create a property by declaring (var x = 1), And thendelete this.xDeletion throws another error.

But this is not complete yet. In fact, attributes created by explicit assignment always cause errors when they are deleted. This is not only an error, but the created attribute seems to have set the dontdelete feature, which of course should not include:


1 This. x = 1;
2 delete this. X; // typeerror: object doesn't support this action
3 typeof X; // "number" (still exists, wasn' t deleted as it shoshould have been !)
4 Delete X; // typeerror: object doesn't support this action
5 typeof X; // "number" (wasn't deleted again)

In contrast to our thinking, non-declared variables (attributes should be created in a Global Object) Create deletable attributes in IE.

X = 1;
Delete X; // true
Typeof X; // "undefined"

However, if you try to use the "This" reference to delete it from the global code (deleteThis. X), a familiar error pops up:

X = 1;
Delete this. X; // typeerror: cannot delete 'this. X'



If we summarize these actionsdelete this.x It seems that it is unsuccessful. When the involved attributes are explicitly declared (this.x = 1 ,deleteWill throw an error. When an attribute is assigned with an undeclared value (x = 1 ) Or statement (var x = 1 When creating an attribute,deleteWill throw another error.

On the other hand, when the involved attributes are explicitly declared (this.x = 1) At creation,deleteX throws an error. If an attribute is declared (var x = 1 . If the attribute is in the undeclared mode (x = 1.

In last September, I was thinking about this issue. Garnett Smith suggested that "the global variable object in IE is used as a JScript object, and the global object has a host for execution ". Garret references Eric lippert's blog entry. We can test these theories through some tests. Please note that,thisAndwindowIt seems to reference the same object (if we believe"===But the variable object (the object in a declared function) is different from this.



1/* in Global Code */
2 function getbase () {return this ;}
4 getbase () === this. getbase (); // false
5 This. getbase () === this. getbase (); // true
6 window. getbase () === this. getbase (); // true 7. Window. getbase () === getbase (); // false




Understanding why things work so well is an unspeakable beauty that I have seen on the InternetdeleteMisunderstanding of operators. For example, in the answer to Stack Overflow (with an outstanding score), it confidently explains: "Delete is supposed to be no-op when target isn't an object property ". Now we understanddeleteThe core of behavior is clearly that the answer is inaccurate.deleteThere is no distinction between variables and attributes (in fact, these are references for deletion). What really cares about is the dontdelete feature (and the attribute exists ).

It is very interesting to see how this misunderstanding affects each other. In the same thread, someone first proposed to directly Delete the variable (unless it is inevalOtherwise it will not take effect), then another person proposes an incorrect correction method-the variable may be deleted globally, but not in the function.

It is best to be careful when interpreting JavaScript on the website, and always grasp the core of the problem.

'Delete' and Host Object

deleteThe algorithm is probably like this:

  1. If the operation is not a reference, returntrue;
  2. If an object does not have a direct attributetrue(We know that an object can be an activation object or a global image );
  3. If an attribute has the dontdelete featurefalse;
  4. Otherwise, delete the attribute and returntrue;

HoweverdeleteOperator behavior is hard to predict. In fact, there are no mistakes: except for a few, the host object is allowed to execute any type of operation behavior (according to specifications), such as read (internal [get] method), write (internal [put] method) ordelete(Internal [delete] method ). This custom [[delete] behavior makes the Host object so messy.

We have seen some weird behavior in IE. If you delete some objects (obviously executed as the host object), an error will be thrown. Some Firefox versions are being deleted.window.locationWill throw an error. When it comes to host objects, you cannot trustdeleteAny value returned. See what happens in Firefox:

1/* "alert" is a direct property of 'window' (if we were to believe 'hasownproperties ')*/
2 window. hasownproperty ('alert '); // true
3 Delete window. Alert; // true
4 typeof window. Alert; // "function"



Deletewindow.alertReturntrue, Although this property has nothing, it should result in this result. It retains a reference (so it should not be returned in the first steptrue), Which is the direct property of the window object (therefore, true cannot be returned in step 2 ). The only waydeleteReturntrueIs to delete the attribute after step 4. However, attributes will never be deleted.

The implication of this story is never trust the Host object.

Es5 strict Mode

Then, the strict mode of ecmascript 5th edition can be obtained. Some restrictions are being introduced whendeleteWhen an operator is a direct reference to a variable, function parameter, or function identifier, a syntaxerror is thrown. In addition, if the attribute contains [[retriable] = false, A typeerror is thrown.



1 (function (FOO ){
2 "use strict"; // enable strict mode within this function
3 var bar;
4 function Baz (){}
5 delete Foo; // syntaxerror (When deleting argument)
6 Delete bar; // syntaxerror (When deleting variable)
7 delete Baz; // syntaxerror (When deleting variable created with function declaration)
8/* 'length' of function instances has {[[retriable]: false }*/
9 Delete (function () {}). length; // typeerror
10 })();


In addition, deleting undeclared variables (in other words, reference not found) also throws syntaxerror.

"Use strict ";
Delete I _dont_exist; // syntaxerror


As you understand, considering that deleting variables, function declarations, and parameters can lead to so many obfuscation, all these restrictions make sense. The strict mode should adopt more positive and descriptive measures, instead of ignoring deletion without sound.



This article is lengthy and I plan to discuss itdeleteDelete the array option and its meaning. You can refer to MDC articles at any time to learn more about the specific explanations (or read the specifications and perform your own experiments ).

This is in JavascriptdeleteBrief Introduction to operator work:

  • Variables and function declarations are either activated object attributes or global object attributes;
  • The attribute has some features, one of which is dontdelete, which determines whether an attribute can be deleted;
  • Variables and function declarations in global and Function Code always have the dontdelete feature;
  • Function parameters are also the attributes of the active object, with the dontdelete feature;
  • Variables and function declarations in eval code always create attributes without the dontdelete feature;
  • New Attributes always have null features (so they do not have the dontdelete feature );
  • Host objects allow responses to deletions, whether or not they wish;

For more information about what is described here, see ECMA-262 3rd edition specification.

I hope you will like this summary and learn new things. All questions, suggestions, and corrections are welcome.

Related reading:

  • Delete Operator
  • Delete operator (Windows Scripting-JScript)

Original article address: Understanding Delete
Reprint address: /? P = 858


Post comments
Back to Top | back to home page
Previous Article: Understanding the delete mechanism in JavaScript (2)
Next article: ASP. NET-based applications unexpectedly exit due to exceptions not handled in. NET Framework 2.0
Full mode (displaying images and code)
View Original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.