Skip to content

Commit 49a0908

Browse files
committed
Merge pull request BonsaiDen#66 from scriptin/master
delete operator
2 parents fd88511 + bd0f12a commit 49a0908

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

doc/en/core/delete.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## The `delete` Operator
2+
3+
In short, it's *impossible* to delete global variables, functions and some other
4+
stuff in JavaScript which have a `DontDelete` attribute set.
5+
6+
### Global code and Function code
7+
8+
When a variable or a function is defined in a global
9+
or a [function scope](#function.scopes) it is a property of either
10+
Activation object or Global object. Such properties have a set of attributes,
11+
one of these is `DontDelete`. Variable and function declarations in global
12+
and function code always create properties with `DontDelete`, therefore
13+
cannot be deleted.
14+
15+
// global variable:
16+
var a = 1; // DontDelete is set
17+
delete a; // false
18+
a; // 1
19+
20+
// normal function:
21+
function f() {} // DontDelete is set
22+
delete f; // false
23+
typeof f; // "function"
24+
25+
// reassigning doesn't help:
26+
f = 1;
27+
delete f; // false
28+
f; // 1
29+
30+
### Explicit properties
31+
32+
There are things which can be deleted normally: these are explicitly set
33+
properties.
34+
35+
// explicitly set property:
36+
var obj = {x: 1};
37+
obj.y = 2;
38+
delete obj.x; // true
39+
delete obj.y; // true
40+
obj.x; // undefined
41+
obj.y; // undefined
42+
43+
In the example above `obj.x` and `obj.y` can be deleted because they have no
44+
`DontDelete` atribute. That's why an example below works too.
45+
46+
// this works fine, except for IE:
47+
var GLOBAL_OBJECT = this;
48+
GLOBAL_OBJECT.a = 1;
49+
a === GLOBAL_OBJECT.a; // true - just a global var
50+
delete GLOBAL_OBJECT.a; // true
51+
GLOBAL_OBJECT.a; // undefined
52+
53+
Here we use a trick to delete `a`. [`this`](#function.this) here refers
54+
to the Global object and we explicitly declare variable `a` as it's property
55+
which allows us to delete it.
56+
57+
IE (at least 6-8) has some bugs, so code above doesn't work.
58+
59+
### Function arguments and built-ins
60+
61+
Functions' normal arguments, [`arguments` object](#function.arguments)
62+
and built-in properties also have `DontDelete` set.
63+
64+
// function arguments and properties:
65+
(function (x) {
66+
67+
delete arguments; // false
68+
typeof arguments; // "object"
69+
70+
delete x; // false
71+
x; // 1
72+
73+
function f(){}
74+
delete f.length; // false
75+
typeof f.length; // "number"
76+
77+
})(1);
78+
79+
### Host objects
80+
81+
Behaviour of `delete` operator can be unpredictable for hosted objects. Due to
82+
specification, host objects are allowed to implement any kind of behavior.
83+
84+
### In conclusion
85+
86+
`delete` operator often has an unexpected behaviour and can be safely used
87+
only for dealing with explicitly set properties on normal objects.

doc/en/index.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"articles": [
5555
"eval",
5656
"undefined",
57-
"semicolon"
57+
"semicolon",
58+
"delete"
5859
]
5960
},
6061
{

0 commit comments

Comments
 (0)