Mercurial > js4py
annotate core-javascript/closures.rst @ 213:0ea90a1819c2 default tip
Custom theming
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Wed, 27 Jun 2012 16:35:54 +0200 |
| parents | f9f6684f9b32 |
| children |
| rev | line source |
|---|---|
| 112 | 1 Closures |
| 2 ======== | |
| 3 | |
| 113 | 4 You'll see closures a lot in JavaScript. The reasons for this are: |
| 5 | |
| 6 #. In JavaScript, :doc:`bind-is-transient`. | |
| 7 | |
| 8 #. It is callback functions attached to DOM elements that respond to | |
| 9 events. | |
| 10 | |
| 11 #. Most code attaches callbacks to individual DOM elements (rather | |
| 12 than delegating to a parent element). | |
| 13 | |
| 14 | |
| 15 Test code | |
| 16 --------- | |
| 17 | |
| 18 Here's the test code for closures. | |
| 19 | |
|
135
f9f6684f9b32
$ hg mv tutorial.js core-javascript-work/ # and amend the rst files also
jf3243
parents:
133
diff
changeset
|
20 .. literalinclude:: /core-javascript-work/tutorial.js |
| 112 | 21 :language: javascript |
| 22 :start-after: start closures | |
| 23 :end-before: end closures | |
| 113 | 24 |
| 25 | |
| 26 Discussion | |
| 27 ---------- | |
| 28 | |
| 29 The factory function creates two functions, *get* and *set*, which | |
| 30 refer to the variable *value* of the factory function. The *get* | |
| 31 function returns the value of this variable, while the *set* function | |
| 32 changes it. | |
| 33 | |
| 34 To test the factory function we create two get-set pairs. We then | |
| 35 test that each *set* operation changes the value return by its partner | |
| 36 *get* operation. | |
| 37 | |
| 38 Finally, we test that the two pairs don't interfere with each other. | |
| 39 | |
| 40 | |
| 41 Explanation | |
| 42 ----------- | |
| 43 | |
| 44 Objects continue to exist so long as a reference remains to them. | |
| 45 When the last reference to an object is removed (or when the object is | |
| 46 part of *cyclic garbage*) the object can be destroyed. When the | |
| 47 return value of a function is stored (say as the value of a variable) | |
| 48 then this return value continues to exist. Similarly, anything | |
| 49 referred to by the return value continues to exist. | |
| 50 | |
| 51 Closures come about when the return value of a function is itself a | |
| 52 function defined within the outer function (or contains references to | |
| 53 such functions). | |
| 54 | |
| 55 Suppose the outer function returns an *inner function* (or in other | |
| 56 words a function that is defined within the outer function). Suppose | |
| 57 also that the inner function uses a variable (or parameter) of the | |
| 58 outer function. In this situation the inner function holds a | |
| 59 reference to the value of this variable. The code in the inner | |
| 60 function can read (or get) this value. It can also write to this | |
| 61 variable. | |
| 62 | |
| 63 One final point. Each call of the outer function creates a new | |
| 64 instantiation of the function variable. | |
| 131 | 65 |
| 66 | |
| 67 Exercises | |
| 68 --------- | |
| 69 | |
| 133 | 70 A programmer, perhaps in a hurry, misses out a *var* in the example |
| 71 above. So now it reads: | |
| 72 | |
| 131 | 73 .. code-block:: javascript |
| 74 | |
| 75 var get_set_factory = function(){ | |
| 76 | |
| 133 | 77 value; // Here's the missing *var*. |
| 131 | 78 |
| 79 | |
| 80 #. Which tests to you expect to pass, and which to fail? | |
| 81 | |
| 82 #. Make this change and run the tests. Which actually fail? | |
| 83 | |
| 84 #. Explain what is happening. | |
| 85 | |
| 86 #. What does this tell us about coding and testing? |
