Mercurial > js4py
diff 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 |
line wrap: on
line diff
--- a/core-javascript/solution-2.rst Mon Oct 31 14:22:00 2011 +0000 +++ b/core-javascript/solution-2.rst Tue Nov 01 08:58:38 2011 +0000 @@ -4,7 +4,7 @@ Here's the code we are studying. The first task is make a loop that does the assignments. -.. literalinclude:: /js/exercise-2.js +.. literalinclude:: /core-javascript-work/exercise-2.js :language: javascript @@ -18,7 +18,7 @@ Here's the solution. There's a bit of overhead in setting up the loop, but the body of the loop is straightforward. -.. literalinclude:: /js/solution-2-a.js +.. literalinclude:: /core-javascript-work/solution-2-a.js :language: javascript @@ -31,7 +31,7 @@ This is a common refactoring pattern, and so is worth learning well. First we give the code, and then the explanation. -.. literalinclude:: /js/solution-2-b.js +.. literalinclude:: /core-javascript-work/solution-2-b.js :language: javascript @@ -56,7 +56,7 @@ the class supplying the *new* operator to create the new instance (and chaos results if the user forgets). -.. literalinclude:: /js/solution-2-c.js +.. literalinclude:: /core-javascript-work/solution-2-c.js :language: javascript A minor point is the trailing sequence of right curly braces. Two are @@ -64,21 +64,28 @@ happens if we put a semicolon there? -A Class class -------------- +A SimpleClass solution +---------------------- Here's another solution to the third task, which does not require the user to supply a *new* operator when creating instances. Instead it uses a factory function, which we call *Class* , that creates an instance constructor function from a prototype object. -.. literalinclude:: /js/solution-2-d.js +.. literalinclude:: /core-javascript-work/solution-2-d.js :language: javascript -A bound method class +Exercise: write test -------------------- +Here's an exercise. Write a JavaScript file that tests this solution. + + +Exercise: bound method class +---------------------------- + + Recall that this-based instance methods are somewhat fragile. In other words, code like this will fail (and make unwanted changes to the global object): @@ -101,3 +108,5 @@ var myclass = {} // The prototype object. // Add methods to myclass. var MyClass = BoundMethodClass(myclass); + +The exercise is to create (and test) a BoundMethodClass factory.
