|
104
|
1 The global object
|
|
|
2 =================
|
|
|
3
|
|
127
|
4 JavaScript has a global object. It is, so to speak, the object of
|
|
|
5 last resort. If there's no other suitable object then JavaScript will
|
|
|
6 use the global object (rather than reporting an error).
|
|
|
7
|
|
|
8 .. note::
|
|
|
9
|
|
|
10 Douglas Crockford writes **JavaScript's global object ... is far and
|
|
|
11 away the worst part of JavaScript's many bad parts.**
|
|
104
|
12
|
|
|
13
|
|
|
14 Getting global
|
|
|
15 ---------------
|
|
|
16
|
|
127
|
17 Here's how to get the global object.
|
|
|
18
|
|
104
|
19 .. code-block:: javascript
|
|
|
20
|
|
|
21 js> return_this = function(){return this;}
|
|
|
22
|
|
|
23 function () {
|
|
|
24 return this;
|
|
|
25 }
|
|
|
26
|
|
|
27 js> global = return_this()
|
|
|
28 [object global]
|
|
127
|
29
|
|
|
30 The programmer who wants to can always obtain access to the global
|
|
|
31 object.
|
|
|
32
|
|
|
33
|
|
|
34 Global variables
|
|
|
35 ----------------
|
|
|
36
|
|
|
37 Global variables are nothing more than attributes of the global
|
|
|
38 object.
|
|
|
39
|
|
|
40 .. code-block:: javascript
|
|
|
41
|
|
104
|
42 js> s = 'Hello world.'
|
|
|
43 Hello world.
|
|
|
44 js> global.s
|
|
|
45 Hello world.
|
|
|
46 js> global.i = 42
|
|
|
47 42
|
|
|
48 js> i
|
|
|
49 42
|
|
127
|
50
|
|
|
51
|
|
|
52 Global pollution
|
|
|
53 ----------------
|
|
|
54
|
|
|
55 It's very easy to inadvertently pollute (change) the global object.
|
|
|
56 All it takes is an assignment to an undeclared variable in a function.
|
|
|
57
|
|
|
58 .. code-block:: javascript
|
|
|
59
|
|
104
|
60 js> pollute = function(n){ i = n; };
|
|
|
61 function (n) {
|
|
|
62 i = n;
|
|
|
63 }
|
|
|
64 js> i
|
|
|
65 42
|
|
|
66 js> pollute(13)
|
|
|
67 js> i
|
|
|
68 13
|
|
127
|
69
|
|
|
70 More global pollution
|
|
|
71 ---------------------
|
|
|
72
|
|
|
73 There's a more subtle way to pollute the global object, which involves :doc:`JavaScript's this object <what-is-this>`.
|
|
|
74
|
|
|
75 To begin with, note that we can push values onto an array.
|
|
|
76
|
|
|
77 .. code-block:: javascript
|
|
|
78
|
|
104
|
79 js> array = []
|
|
|
80
|
|
|
81 js> array.push(1, 2, 3)
|
|
|
82 3
|
|
|
83 js> array
|
|
|
84 1,2,3
|
|
127
|
85
|
|
|
86 Let's try using *array.push* as a stand-alone function.
|
|
|
87
|
|
|
88 .. code-block:: javascript
|
|
|
89
|
|
104
|
90 js> p = array.push
|
|
|
91 function push() { [native code for Array.push, arity=1] }
|
|
|
92
|
|
|
93 js> p(4, 5, 6)
|
|
|
94 3
|
|
127
|
95
|
|
|
96 The push function returns the length of the object it has just pushed
|
|
|
97 to. So the value *3* above is a signal that all is not well. And
|
|
|
98 indeed it is not. The original array is unchanged, and the global
|
|
|
99 object has an entry at 0.
|
|
|
100
|
|
|
101 .. code-block:: javascript
|
|
|
102
|
|
104
|
103 js> array
|
|
|
104 1,2,3
|
|
|
105 js>
|
|
|
106 js> global[0]
|
|
|
107 4
|
|
127
|
108
|
|
|
109 It's also got a length!
|
|
|
110
|
|
|
111 .. code-block:: javascript
|
|
|
112
|
|
|
113 js> global.length
|
|
|
114 3
|
|
|
115
|
|
|
116
|
|
|
117 Explanation
|
|
|
118 -----------
|
|
|
119
|
|
|
120 This behaviour is a consequence of :doc:`what-is-this` and
|
|
|
121 :doc:`bind-is-transient`, together with JavaScript's no-exceptions
|
|
|
122 design.
|