comparison core-javascript/solution-2.rst @ 150:5118d0e38ba4 core-javascript-latex

$ hg merge core-javascript # into core-javascript-latex
author jf3243
date Tue, 01 Nov 2011 08:58:38 +0000
parents 6c98b9680b2a
children
comparison
equal deleted inserted replaced
136:5666f217df39 150:5118d0e38ba4
2 ============ 2 ============
3 3
4 Here's the code we are studying. The first task is make a loop that 4 Here's the code we are studying. The first task is make a loop that
5 does the assignments. 5 does the assignments.
6 6
7 .. literalinclude:: /js/exercise-2.js 7 .. literalinclude:: /core-javascript-work/exercise-2.js
8 :language: javascript 8 :language: javascript
9 9
10 10
11 Making a loop 11 Making a loop
12 ------------- 12 -------------
16 hard-coded and we want it to vary during the loop. 16 hard-coded and we want it to vary during the loop.
17 17
18 Here's the solution. There's a bit of overhead in setting up the 18 Here's the solution. There's a bit of overhead in setting up the
19 loop, but the body of the loop is straightforward. 19 loop, but the body of the loop is straightforward.
20 20
21 .. literalinclude:: /js/solution-2-a.js 21 .. literalinclude:: /core-javascript-work/solution-2-a.js
22 :language: javascript 22 :language: javascript
23 23
24 24
25 Factory function 25 Factory function
26 ---------------- 26 ----------------
29 keys as a parameter. 29 keys as a parameter.
30 30
31 This is a common refactoring pattern, and so is worth learning well. 31 This is a common refactoring pattern, and so is worth learning well.
32 First we give the code, and then the explanation. 32 First we give the code, and then the explanation.
33 33
34 .. literalinclude:: /js/solution-2-b.js 34 .. literalinclude:: /core-javascript-work/solution-2-b.js
35 :language: javascript 35 :language: javascript
36 36
37 37
38 Here's what we've done. We created a wrapper function, with *keys* as 38 Here's what we've done. We created a wrapper function, with *keys* as
39 its only parameter. This will be our factory function. We've placed 39 its only parameter. This will be our factory function. We've placed
54 54
55 Here's a function prototype based solution. It relies on the user of 55 Here's a function prototype based solution. It relies on the user of
56 the class supplying the *new* operator to create the new instance (and 56 the class supplying the *new* operator to create the new instance (and
57 chaos results if the user forgets). 57 chaos results if the user forgets).
58 58
59 .. literalinclude:: /js/solution-2-c.js 59 .. literalinclude:: /core-javascript-work/solution-2-c.js
60 :language: javascript 60 :language: javascript
61 61
62 A minor point is the trailing sequence of right curly braces. Two are 62 A minor point is the trailing sequence of right curly braces. Two are
63 followed by a semicolon, but the middle one is not. Why? What 63 followed by a semicolon, but the middle one is not. Why? What
64 happens if we put a semicolon there? 64 happens if we put a semicolon there?
65 65
66 66
67 A Class class 67 A SimpleClass solution
68 ------------- 68 ----------------------
69 69
70 Here's another solution to the third task, which does not require the 70 Here's another solution to the third task, which does not require the
71 user to supply a *new* operator when creating instances. Instead it 71 user to supply a *new* operator when creating instances. Instead it
72 uses a factory function, which we call *Class* , that creates an 72 uses a factory function, which we call *Class* , that creates an
73 instance constructor function from a prototype object. 73 instance constructor function from a prototype object.
74 74
75 .. literalinclude:: /js/solution-2-d.js 75 .. literalinclude:: /core-javascript-work/solution-2-d.js
76 :language: javascript 76 :language: javascript
77 77
78 78
79 A bound method class 79 Exercise: write test
80 -------------------- 80 --------------------
81
82 Here's an exercise. Write a JavaScript file that tests this solution.
83
84
85 Exercise: bound method class
86 ----------------------------
87
81 88
82 Recall that this-based instance methods are somewhat fragile. In 89 Recall that this-based instance methods are somewhat fragile. In
83 other words, code like this will fail (and make unwanted changes to 90 other words, code like this will fail (and make unwanted changes to
84 the global object): 91 the global object):
85 92
99 .. code-block:: javascript 106 .. code-block:: javascript
100 107
101 var myclass = {} // The prototype object. 108 var myclass = {} // The prototype object.
102 // Add methods to myclass. 109 // Add methods to myclass.
103 var MyClass = BoundMethodClass(myclass); 110 var MyClass = BoundMethodClass(myclass);
111
112 The exercise is to create (and test) a BoundMethodClass factory.