Mercurial > js4py
annotate core-javascript/solution-1.rst @ 213:0ea90a1819c2 default tip
Custom theming
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Wed, 27 Jun 2012 16:35:54 +0200 |
| parents | 58e4f484903f |
| children |
| rev | line source |
|---|---|
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
1 Solution one |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
2 ============ |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
3 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
4 Here's the code we're studying. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
5 |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
106
diff
changeset
|
6 .. literalinclude:: /core-javascript-work/exercise-1.js |
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
7 :language: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
8 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
9 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
10 Verbal summary |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
11 -------------- |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
12 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
13 We are making sure that there's an object AAA and giving it an |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
14 attribute AAA.fff, which is a function. We won't say more about the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
15 function in this summary. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
16 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
17 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
18 Hoisting once-only arguments |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
19 ---------------------------- |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
20 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
21 First, we'll hoist the argument to the anonymous function into its |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
22 body. We can always do this, without changing the meaning, provided the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
23 function is called only once. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
24 |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
106
diff
changeset
|
25 .. literalinclude:: /core-javascript-work/solution-1-a.js |
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
26 :language: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
27 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
28 There, that's better. We no longer have to read to the end of the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
29 function to find out what BBB is (and in particular its relationship |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
30 with AAA). |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
31 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
32 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
33 What is AAA? |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
34 ------------ |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
35 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
36 Next, we'll hide (or ignore) the assigment to BBB.fff. This |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
37 assignment sets an attribute on the object pointed to by the variable |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
38 BBB. In other words, it mutates the object pointed to by BBB, but the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
39 variable BBB still points to the same object as it did before. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
40 |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
106
diff
changeset
|
41 .. literalinclude:: /core-javascript-work/solution-1-b.js |
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
42 :language: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
43 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
44 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
45 If AAA starts out as an object then the expression |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
46 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
47 .. code-block:: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
48 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
49 AAA || {} |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
50 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
51 evaluates to AAA, while if AAA is undefined then it evaluates to the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
52 newly created object literal (the left-and-right curly braces). In |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
53 either case, this value is assigned to the local variable BBB, which |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
54 is then returned by the anonymous function and bound to AAA. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
55 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
56 Put another way, if AAA starts off as undefined then it is bound to a |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
57 new object, while if it starts off as an object then it finishes as |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
58 the same object. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
59 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
60 Therefore, our truncated version of the original exercise is equivalent to |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
61 |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
106
diff
changeset
|
62 .. literalinclude:: /core-javascript-work/solution-1-c.js |
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
63 :language: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
64 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
65 This idiom is very common in JavaScript, and is (as here) a |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
66 consequence of using objects as namespaces (which is better than using |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
67 the global object) and defensiveness about the order in which files |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
68 are loaded. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
69 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
70 Here's a command line session that shows this equivalence. To begin |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
71 with AAA is undefined and the assignment binds AAA to a new object. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
72 For the second assignment AAA is defined and the assignment binds AAA |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
73 to the object is already bound to. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
74 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
75 .. code-block:: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
76 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
77 js> var AAA = AAA || {} |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
78 js> AAA |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
79 [object Object] |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
80 js> pointer = AAA |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
81 [object Object] |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
82 js> var AAA = AAA || {} |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
83 js> pointer === AAA |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
84 true |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
85 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
86 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
87 The assignment to BBB.fff |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
88 ------------------------- |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
89 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
90 Reading the code, it looks as if BBB is local to the anonymous |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
91 function. As a variable it is, but its value is not. The value of |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
92 BBB is bound to the global object AAA at the end of the function call. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
93 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
94 The following is almost equivalent to our original JavaScript code. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
95 Note that BBB.hhh has been changed to AAA.hhh. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
96 |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
106
diff
changeset
|
97 .. literalinclude:: /core-javascript-work/solution-1-d.js |
|
106
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
98 :language: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
99 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
100 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
101 Caveat |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
102 ------ |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
103 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
104 Why did I say *almost equivalent*? Because we might subsequently do |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
105 something like |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
106 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
107 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
108 .. code-block:: javascript |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
109 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
110 var CCC = AAA; |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
111 AAA = undefined; |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
112 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
113 For the original code the closure variable BBB still exists and points |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
114 to the same object as CCC, even though AAA is undefined. Thus, the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
115 reference to BBB.hhh in the original code will continue to work. |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
116 |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
117 For the revised code, when the value of AAA is set to undefined the |
|
bf394cf588c8
Add solution-1.rst and companion JavaScript example files.
jf3243
parents:
diff
changeset
|
118 reference to AAA.hhh will fail. |
