Mercurial > js4py
annotate core-javascript/exercise-2.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 |
|---|---|
| 107 | 1 Exercise two |
| 2 ============ | |
| 3 | |
| 4 Here's some more JavaScript code I found. I've changed the names of | |
| 5 everything to hide the origin of the code. But it's not something | |
| 6 I've made up. | |
| 7 | |
|
145
58e4f484903f
Fix errors caused by mv in previous commit.
Jonathan Fine <jfine@pytex.org>
parents:
130
diff
changeset
|
8 .. literalinclude:: /core-javascript-work/exercise-2.js |
| 107 | 9 :language: javascript |
| 10 | |
| 11 The exercise is | |
| 12 | |
| 13 #. Rewrite the function copy_attributes so that it makes a loop over | |
| 14 | |
| 15 .. code-block:: javascript | |
| 16 | |
| 108 | 17 var keys = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh']; |
| 107 | 18 |
| 19 #. Write a copy_attributes_factory function so that we can write | |
| 20 | |
| 21 .. code-block:: javascript | |
| 22 | |
| 23 var copy_attributes = copy_attributes_factory(keys); | |
| 24 | |
| 25 #. Write a Fields class such that something like this will work: | |
| 26 | |
| 27 .. code-block:: javascript | |
| 28 | |
| 29 var aaa = Fields(keys); | |
| 30 aaa.copy_attributes(src, tgt); | |
| 31 | |
| 32 | |
| 33 Hints | |
|
130
ff23e32bbe29
Sort out table of contents, and various minor changes.
jf3243
parents:
108
diff
changeset
|
34 ----- |
| 107 | 35 |
| 36 #. In JavaScript attribute access and item access are the same. | |
| 37 First we use item access to set: | |
| 38 | |
| 39 .. code-block:: javascript | |
| 40 | |
| 41 js> a = {} | |
| 42 [object Object] | |
| 43 js> key = 'attrname' | |
| 44 attrname | |
| 45 js> a[key] = 42 | |
| 46 42 | |
| 47 | |
| 48 and then we use attribute access to get: | |
| 49 | |
| 50 .. code-block:: javascript | |
| 51 | |
| 52 js> a.attrname | |
| 53 42 | |
| 54 | |
| 55 #. This applies even for function (aka method) calls. | |
| 56 | |
| 108 | 57 #. Have the factory function store the keys in a closure. |
| 107 | 58 |
| 59 #. There are many ways of writing Fields. Choose one that suits you. |
