1. Jonathan Fine
  2. javascript-for-python-programmers

Commits

Jonathan Fine  committed 53d9c4a

gotcha.txt: Swapped 'Missing that' and 'Missing closure'. Improved Makefile.

  • Participants
  • Parent commits e0b35dd
  • Branches default

Comments (0)

Files changed (4)

File Makefile

View file
  • Ignore whitespace
 clean:
 	-rm -rf $(BUILDDIR)/*
 
-html:
+source/gotcha.rst: txt/gotcha.txt
+	@python py/gotcha.py
+
+html: source/gotcha.rst
 	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
 	@echo
 	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

File generated/gotcha-answers.txt

View file
  • Ignore whitespace
 
    :ref:`Question <missing-var-gotcha>`.
 
+.. _missing-closure-gotcha-answer:
+
+**Missing closure**
+  
+   .. code-block:: javascript
+
+      // Add handlers: click on item to show item number.
+      var make_and_add_handlers(items){
+      
+          for(var i=0; i < items.length; i++){
+      
+             items[i].onclick = function(){
+                 alert("This is item " + i);
+             };
+          }   
+      }
+
+   This code, when run, gives all items the same number (namely number of
+   items less one).  This is because only one value of *i* is captured.
+   (It is not because all items are getting the same onclick function.
+   They are getting different functions, but with identical behaviour.)
+   
+   Here's one solution.  In the main function instead write
+   
+   .. code-block:: javascript
+   
+      items[i].onclick = alert_n(i);
+   
+   where we have: 
+   
+   .. code-block:: javascript
+   
+      var alert_n(n){
+          return function(){
+              alert("This is item " + n);
+          };
+      };
+   
+   When we do this, each onclick function captures its own copy of the
+   argument *i* to *alert_n*.  (Closure capture function parameters in
+   the same way as it captures function variables.)
+
+   :ref:`Question <missing-closure-gotcha>`.
+
 .. _missing-that-gotcha-answer:
 
 **Missing that**
    (Following the custom means no further explanation is required.)
 
    :ref:`Question <missing-that-gotcha>`.
-
-.. _missing-closure-gotcha-answer:
-
-**Missing closure**
-  
-   .. code-block:: javascript
-
-      // Add handlers: click on item to show item number.
-      var make_and_add_handlers(items){
-      
-          for(var i=0; i < items.length; i++){
-      
-             items[i].onclick = function(){
-                 alert("This is item " + i);
-             };
-          }   
-      }
-
-   This code, when run, gives all items the same number (namely number of
-   items less one).  This is because only one value of *i* is captured.
-   (It is not because all items are getting the same onclick function.
-   They are getting different functions, but with identical behaviour.)
-   
-   Here's one solution.  In the main function instead write
-   
-   .. code-block:: javascript
-   
-      items[i].onclick = alert_n(i);
-   
-   where we have: 
-   
-   .. code-block:: javascript
-   
-      var alert_n(n){
-          return function(){
-              alert("This is item " + n);
-          };
-      };
-   
-   When we do this, each onclick function captures its own copy of the
-   argument *i* to *alert_n*.  (Closure capture function parameters in
-   the same way as it captures function variables.)
-
-   :ref:`Question <missing-closure-gotcha>`.

File generated/gotcha-questions.txt

View file
  • Ignore whitespace
 
    :ref:`Answer <missing-var-gotcha-answer>`.
 
+.. _missing-closure-gotcha:
+
+**Missing closure**
+  
+   .. code-block:: javascript
+
+      // Add handlers: click on item to show item number.
+      var make_and_add_handlers(items){
+      
+          for(var i=0; i < items.length; i++){
+      
+             items[i].onclick = function(){
+                 alert("This is item " + i);
+             };
+          }   
+      }
+
+   :ref:`Answer <missing-closure-gotcha-answer>`.
+
 .. _missing-that-gotcha:
 
 **Missing that**
       Name = Charles
 
    :ref:`Answer <missing-that-gotcha-answer>`.
-
-.. _missing-closure-gotcha:
-
-**Missing closure**
-  
-   .. code-block:: javascript
-
-      // Add handlers: click on item to show item number.
-      var make_and_add_handlers(items){
-      
-          for(var i=0; i < items.length; i++){
-      
-             items[i].onclick = function(){
-                 alert("This is item " + i);
-             };
-          }   
-      }
-
-   :ref:`Answer <missing-closure-gotcha-answer>`.

File txt/gotcha.txt

View file
  • Ignore whitespace
 
 
 &&&
+Missing closure
+===
+// Add handlers: click on item to show item number.
+var make_and_add_handlers(items){
 
+    for(var i=0; i < items.length; i++){
+
+       items[i].onclick = function(){
+           alert("This is item " + i);
+       };
+    }   
+}
+===
+
+This code, when run, gives all items the same number (namely number of
+items less one).  This is because only one value of *i* is captured.
+(It is not because all items are getting the same onclick function.
+They are getting different functions, but with identical behaviour.)
+
+Here's one solution.  In the main function instead write
+
+.. code-block:: javascript
+
+   items[i].onclick = alert_n(i);
+
+where we have: 
+
+.. code-block:: javascript
+
+   var alert_n(n){
+       return function(){
+           alert("This is item " + n);
+       };
+   };
+
+When we do this, each onclick function captures its own copy of the
+argument *i* to *alert_n*.  (Closure capture function parameters in
+the same way as it captures function variables.)
+
+&&&
 Missing that
 ===
 // Return fn that does something (and logs the instance).
 It is customary to write **that = that** to achieve this effect.
 (Following the custom means no further explanation is required.)
 
-&&&
-Missing closure
-===
-// Add handlers: click on item to show item number.
-var make_and_add_handlers(items){
-
-    for(var i=0; i < items.length; i++){
-
-       items[i].onclick = function(){
-           alert("This is item " + i);
-       };
-    }   
-}
-===
-
-This code, when run, gives all items the same number (namely number of
-items less one).  This is because only one value of *i* is captured.
-(It is not because all items are getting the same onclick function.
-They are getting different functions, but with identical behaviour.)
-
-Here's one solution.  In the main function instead write
-
-.. code-block:: javascript
-
-   items[i].onclick = alert_n(i);
-
-where we have: 
-
-.. code-block:: javascript
-
-   var alert_n(n){
-       return function(){
-           alert("This is item " + n);
-       };
-   };
-
-When we do this, each onclick function captures its own copy of the
-argument *i* to *alert_n*.  (Closure capture function parameters in
-the same way as it captures function variables.)