Mercurial > js4py
changeset 209:f7362edfc7ce
generated answers
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Thu, 17 May 2012 11:47:53 +0200 |
| parents | b1f5aa2daf53 |
| children | 622b8eec7dd6 |
| files | generated/gotcha-answers.txt |
| diffstat | 1 files changed, 19 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/generated/gotcha-answers.txt Thu May 17 11:47:26 2012 +0200 +++ b/generated/gotcha-answers.txt Thu May 17 11:47:53 2012 +0200 @@ -9,7 +9,7 @@ ... } - Ordinary equality (==) can be surprising and so use **===** instead: + Ordinary equality (``==``) can be surprising and so use ``===`` instead: .. code-block:: javascript @@ -18,7 +18,7 @@ '' == '0' // False. Not transitive! undefined == null // True - I don't know of any situations where == is preferred. To compare two + I don't know of any situations where ``==`` is preferred. To compare two values after conversion to string (respectively number) make it explicit by writing respectively: @@ -39,7 +39,7 @@ y = a + (b + c) x == y // True or False? When? - In **str + num** the *num* is silently converted to a string. + In **string + number** the *number* is silently converted to a string. .. code-block:: javascript @@ -52,7 +52,7 @@ 15 - In **num + str** the *num* is again silently converted to a string. + In **number + string** the *number* is again silently converted to a string. .. code-block:: javascript @@ -103,7 +103,7 @@ [7, 8, 9] ] - In Python you'd get an TypeError from this code, as in: + In Python you'd get an ``TypeError`` from this code, as in: .. code-block:: python @@ -172,7 +172,7 @@ tmp = x.slice; // What happens here? b = tmp(1, 3); // What happens here? - Python uses bound methods and so in Python *a.b* is a first class + Python uses bound methods and so in Python ``a.b`` is a first class object. .. code-block:: python @@ -233,14 +233,14 @@ total = a + b return total / 2.0 - works because in Python *total* is recognised as being local to the - function *average*. + works because in Python ``total`` is recognised as being local to the + function ``average``. In JavaScript variables are global unless explicitly declared as local. The sample code has a interesting side effect. It assigns to the - global variable *total*. + global variable ``total``. .. code-block:: python @@ -269,7 +269,7 @@ } 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. + 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.) @@ -290,7 +290,7 @@ }; When we do this, each onclick function captures its own copy of the - argument *i* to *alert_n*. (Closure capture function parameters in + argument ``i`` to ``alert_n``. (Closure capture function parameters in the same way as it captures function variables.) :ref:`Question <missing-closure-gotcha>`. @@ -319,7 +319,7 @@ js> fn(...) // Logging to go to console. Name = Charles - The code above (provided the global object does not have a *name* + The code above (provided the global object does not have a ``name`` attribute) produces: .. code-block:: javascript @@ -327,7 +327,7 @@ js> fn(...) // Logging to go to console. Name = undefined - When a JavaScript function executes, what *this* refers to depends on + When a JavaScript function executes, what ``this`` refers to depends on the calling context. In particular, in .. code-block:: javascript @@ -340,7 +340,7 @@ return this.attribute; } - the two occurences of *this* usually refer to completely different + the two occurences of ``this`` usually refer to completely different objects. To fix instead write: .. code-block:: javascript @@ -354,12 +354,12 @@ return that.attribute; } - This works because *that* does not have the special role *this* has. - In the returned function, *that* refers to the same object as *that* - does in main part of *fn2*, which in turn refers to the same object as - the *this* object in the body. + This works because ``that`` does not have the special role ``this`` has. + In the returned function, ``that`` refers to the same object as ``that`` + does in main part of ``fn2``, which in turn refers to the same object as + the ``this`` object in the body. - It is customary to write **that = that** to achieve this effect. + It is customary to write ``that=that`` to achieve this effect. (Following the custom means no further explanation is required.) :ref:`Question <missing-that-gotcha>`.
