-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathDeleteVar.qhelp
More file actions
38 lines (30 loc) · 1000 Bytes
/
DeleteVar.qhelp
File metadata and controls
38 lines (30 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
The <code>delete</code> operator should only be used to delete properties from objects. Using it
to delete variables makes code hard to maintain and will break in strict mode.
</p>
</overview>
<recommendation>
<p>
If the variable you are deleting is a global variable, this is a sign that your code relies too
much on global state. Try encapsulating this global state by means of one of the module patterns
introduced in <i>JavaScript: The Good Parts</i>.
</p>
</recommendation>
<example>
<p>
In the following code snippet, <code>delete</code> is used to clean up the global <code>cache</code>
variable used by function <code>get</code>.
</p>
<sample src="examples/DeleteVar.js" />
<p>
It would be clearer to wrap the whole module into a closure like this (which also avoids exposing
function <code>compute</code> to the outside world):
</p>
<sample src="examples/DeleteVarGood.js" />
</example>
</qhelp>