In javascript, we sometimes use delete to delete objects. However, we may not know the details of the delete operation. Yesterday, I saw kangax's article on delete analysis, which benefited a lot. This article will translate the essence of the article and share it with you.
- Principle
- Code Type
- Execution Context
- Activate object/variable object
- Attribute features
- Built-in object and DontDelete
- Undeclared value assignment
- Firebug confusions
- Use eval to delete a variable
- Browser compatibility
- Gecko DontDelete bug
- IE bugs
- Misunderstanding
- 'Delete' and Host Object
- ES5 strict Mode
- Summary
Principle
Why can we delete the attributes of an object?
var o = { x: 1 };
delete o.x; // true
o.x; // undefined
However, variables declared like this do not work:
var x = 1;
delete x; // false
x; // 1
Or the declared function:
function x(){}
delete x; // false
typeof x; // "function"
Note: When an attribute cannot be deleted,deleteOnly false is returned.
To understand this, we need to first master concepts such as variable instantiation and attribute features-unfortunately these are rarely mentioned in javascript books. I will try to revisit these concepts in the following sections. It is not difficult to understand them. If you don't care why they are running, you can skip this chapter at will.
Code Type
There are three types of executable code in ECMAScript:Global code),Function code)AndEval code. These types have some self-descriptions, but here is a brief overview:
- When a piece of source code body is regarded as a program, it is executed in the global scope and treatedGlobal code). In a browser environment, the content in the SCRIPT element is often parsed as a program. Therefore, it is evaluated as a global code.
- Any Code directly executed within a function is obviously treatedFunction code). The content of the event attribute in the browser red (for example:
<p onclick="...">) Is usually parsed as Function code;
- Finally, the text provided to the built-in function eval () is treatedEval code). We will soon see that this type is very special.
- 8 pages in total:
- Previous Page
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- Next Page