Mercurial > js4py
changeset 211:4e6cfa066d1a
Use double backticks for inline code everywhere
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Fri, 18 May 2012 16:56:15 +0200 |
| parents | 622b8eec7dd6 |
| children | 2db89f138093 |
| files | source/classes.rst source/counters-memory.rst source/functions-and-this.rst source/functions.rst source/index.rst source/javascript-objects.rst source/know-in-python.rst source/objects.rst |
| diffstat | 8 files changed, 104 insertions(+), 130 deletions(-) [+] |
line wrap: on
line diff
--- a/source/classes.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/classes.rst Fri May 18 16:56:15 2012 +0200 @@ -16,8 +16,8 @@ As well as sharing class data, each instance of a class has its own data, which is added to the instance as part of its creation. In -Python, this is usually done using the __init__ method (although -sometimes __new__ is also used). +Python, this is usually done using the ``__init__`` method (although +sometimes ``__new__`` is also used). Point in Python --------------- @@ -42,12 +42,12 @@ Point in JavaScript --------------------- +------------------- To implement a similar class in JavaScript we need a function Point that returns a point, and an object that is a parent for all the -points created by the Point function. In Python we had a single -object, namely the class Point, that performed both roles. In +points created by the ``Point`` function. In Python we had a single +object, namely the class ``Point``, that performed both roles. In JavaScript we need a constructor function and a parent object. @@ -71,7 +71,7 @@ this.y += dy; }; -See [link] for why we use this rather than self. +See [link] for why we use ``this`` rather than ``self``. Constructor @@ -89,15 +89,15 @@ }; -In production we would use *arguments* and *apply* rather than -directly calling *__init__*. Do you know why? +In production we would use ``arguments`` and ``apply`` rather than +directly calling ``__init__``. Do you know why? Advanced features ----------------- This can be omitted at a first reading. -Improved toString +Improved ``toString`` ^^^^^^^^^^^^^^^^^ In Python we have, for example: @@ -121,7 +121,7 @@ ^^^^^^^^^^^^^^^^^^^ Here is a factory function for producing constructor functions, -such as Point above. +such as ``Point`` above. .. code-block:: javascript
--- a/source/counters-memory.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/counters-memory.rst Fri May 18 16:56:15 2012 +0200 @@ -20,8 +20,8 @@ independent garbage collector. This means that if a DOM node *d* held a reference to a JavaScript -object *j* and also that *j* held a reference to *d* then neither -garbage collector could collect *j* or *d*. What's worse, even when +object ``j`` and also that ``j`` held a reference to ``d`` then neither +garbage collector could collect ``j`` or ``d``. What's worse, even when the page was unloaded IE did not reclaim this memory. Thus, prior to IE8, JavaScript could cause Internet Explorer to leak @@ -38,7 +38,7 @@ x = [1, 2, 3, 4, 5, 6, 7]; x = null; -The second assignment to *x* ensures that the original array (created +The second assignment to ``x`` ensures that the original array (created by the array literal) can no longer be accessed, and so it can be garbage collected. @@ -49,20 +49,16 @@ The whole of the discussion here reduces to two things -* The execution of the context of the namespace anonymous function +* The execution of the context of the namespace of anonymous function continues to exist after the function has completed execution. * If the line - .. code-block: javascript + .. code-block:: javascript + element.onclick = onclick_factory(models); - were replaced by - - .. code-block: javascript - element.onclick = onclick_factory(models); - - then after the execution of the namespace anonymous function all the + were removed, then after the execution of the anonymous function all the JavaScript objects it created would be garbage collected. The trick is to allow garbage collection to take place by releasing, @@ -71,17 +67,17 @@ Residue ------- -The *element* DOM node continues to exist, as it can be reached from -the *document* node. It has an *onclick* attribute, which is the -function created by the *onclick_factory*, with *models* as the +The ``element`` DOM node continues to exist, as it can be reached from +the ``document`` node. It has an ``onclick`` attribute, which is the +function created by the ``onclick_factory``, with ``models`` as the argument. So that function is not garbage collected and neither is -*models*. +``models``. This is just as it should be. Anything that can be reached from the rendered web page is not garbage collected. It has to be there so that click has the desired effect. -The function *element.onclick* retains a reference to *models* just as +The function ``element.onclick`` retains a reference to ``models`` just as surely as executing .. code-block:: javascript @@ -89,13 +85,13 @@ var f = function(arg){ return [1, 2, 3, arg] }; var y = f(x); -causes *y* to retain a reference to the value of *x*. +causes ``y`` to retain a reference to the value of ``x``. -Finally, *models* contains references to the *Counter* instances and -thus, by the hidden prototype reference, to the *counter* prototype +Finally, ``models`` contains references to the ``Counter`` instances and +thus, by the hidden prototype reference, to the ``counter`` prototype object. -The *counter* prototype object lies in the execution context of the +The ``counter`` prototype object lies in the execution context of the anonymous function, and as it happend that keeps alive the whole of the execution context. @@ -103,22 +99,22 @@ ----------------- Notice that the JavaScript holds only two reference to DOM nodes, -namely *document* and *example*. However, the line of code +namely ``document`` and ``example``. However, the line of code .. code-block:: javascript element = undefined; // Release reference to DOM node. -means that the JavaScript no longer holds a reference to *element*. +means that the JavaScript no longer holds a reference to ``element``. -The DOM node *example* holds a reference, via its *onclick* function, -to the JavaScript on the page, and in particular to the *Counter* +The DOM node ``example`` holds a reference, via its ``onclick`` function, +to the JavaScript on the page, and in particular to the ``Counter`` instances. This cannot be avoided, because we want certain DOM nodes to change the state of the counters. However, the converse is not true. We can write the JavaScript code so that it has but one reference to a DOM object, namely the -*document* node held for example as a property of the *global* object. +``document`` node held for example as a property of the ``global`` object. Summary -------
--- a/source/functions-and-this.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/functions-and-this.rst Fri May 18 16:56:15 2012 +0200 @@ -1,19 +1,19 @@ Functions and *this* ==================== -Understanding *this* in JavaScript is hard, but also vital. It -corresponds, roughly, to the use of **self** in Python. But there are +Understanding ``this`` in JavaScript is hard, but also vital. It +corresponds, roughly, to the use of ``self`` in Python. But there are big differences. We start with the pitfalls, then give the valuable use, and then give another pitfall. -We need *this* to do efficient object oriented programming in +We need ``this`` to do efficient object oriented programming in JavaScript. But there are potential pitfalls, which we describe -first. We then describe the useful applications of *this*. +first. We then describe the useful applications of ``this``. .. note:: It's not possible to get far in JavaScript without understanding - **this**. But understanding **this** is helps you become a + ``this``. But understanding ``this`` is helps you become a confident expert. Please persevere until you get it. Pitfalls @@ -21,11 +21,11 @@ Here we describe some of the traps that await the unwary. -*this* is a keyword +``this`` is a keyword ^^^^^^^^^^^^^^^^^^^ -Unlike Python's *self*, in JavaScript *this* is a keyword. It is not -possible to explicitly assign a value to *this*. +Unlike Python's ``self``, in JavaScript ``this`` is a keyword. It is not +possible to explicitly assign a value to ``this``. .. code-block:: javascript @@ -42,22 +42,22 @@ a = new_value; // Sometimes called 'rebinding'. -But *this* is not an identifier. It is a keyword, and +But ``this`` is not an identifier. It is a keyword, and .. code-block:: javascript this = new_value; -is a syntax error. Good editors highlight *this* to point out its +is a syntax error. Good editors highlight ``this`` to point out its special role. -Mutating *this* -^^^^^^^^^^^^^^^ +Mutating ``this`` +^^^^^^^^^^^^^^^^^ -Even though we cannot assign to *this*, we can mutate the this-object. -It is best to think of *this* as quasi-fixed but mutable object. -(There are ways of changing what *this* refers to, but not in the body +Even though we cannot assign to ``this``, we can mutate the ``this``-object. +It is best to think of ``this`` as quasi-fixed but mutable object. +(There are ways of changing what ``this`` refers to, but not in the body of an executing function.) .. code-block:: javascript @@ -70,19 +70,19 @@ js> this.name // We have mutated *this*. gotcha -The keyword *this* is **bound** to the same object as before, but that +The keyword ``this`` is **bound** to the same object as before, but that object has been **mutated**. (There are two ways to 'change -<identifier>', namely rebinding and mutation. But rebinding of *this* +<identifier>', namely rebinding and mutation. But rebinding of ``this`` is not allowed.) -Global *this* gotcha -^^^^^^^^^^^^^^^^^^^^ +Global ``this`` gotcha +^^^^^^^^^^^^^^^^^^^^^^ JavaScript has a global object, which roughly corresponds to the -__main__ module in Python. However, unlike __main__, we should try to +``__main__`` module in Python. However, unlike ``__main__``, we should try to avoid using and changing JavaScript's global object. [link] -Let's continue the previous example. We just mutated the *this* +Let's continue the previous example. We just mutated the ``this`` object, by adding a name to it. But what object did we change? In fact, we've just added a new global value. @@ -97,14 +97,14 @@ .. note:: - Whenever you mutate **this**, be sure you know what - object **this** is. + Whenever you mutate ``this``, be sure you know what + object ``this`` is. -Methods and *this* ------------------- +Methods and ``this`` +-------------------- -There are two ways to set *this*, namely implicit and explict. Here +There are two ways to set ``this``, namely implicit and explict. Here we describe the implicit method. The rule is quite simple. Suppose we execute @@ -113,12 +113,12 @@ result = obj.method() -where *obj.method* is a function. (If *obj.method* is not a function +where ``obj.method`` is a function. (If ``obj.method`` is not a function we get a runtime error.) -In this situation, *this* is bound to *obj* during this execution of -*method*. Here is an example: +In this situation, ``this`` is bound to ``obj`` during this execution of +``method``. Here is an example: .. code-block:: javascript
--- a/source/functions.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/functions.rst Fri May 18 16:56:15 2012 +0200 @@ -24,9 +24,9 @@ as the template, where of course you get to choose the number and names of the parameters. Unlike Python, you can't provide default -values for the arguments, nor can you provide **\*args** and -**\*\*kwargs**. However, as we will see, there is the pseudo-variable -*arguments* in JavaScript. +values for the arguments, nor can you provide ``*args`` and +``**kwargs``. However, as we will see, there is the pseudo-variable +``arguments`` in JavaScript. This template can be adapted @@ -37,7 +37,7 @@ }; to define an attribute or method of an object. This is most commonly -done when *obj* is a prototype. +done when ``obj`` is a prototype. You can also put a function in an object literal, as in @@ -95,7 +95,7 @@ x = my_fn(a, b, c); The this-object is set to the :doc:`global-object`, which causes a -problem only if the body of the function refers to *this*. Many +problem only if the body of the function refers to ``this``. Many functions are intended to be used in this way. @@ -109,8 +109,8 @@ x = obj.my_fn(a, b, c); // Usual form. x = obj['my_fn'](a, b, c); // Variant form. -The two forms are completely equivalent. The this-object is set to -the *obj*. +The two forms are completely equivalent. The ``this``-object is set to +the ``obj``. The two following calls are always equivalent: @@ -133,13 +133,13 @@ whereas in Python they always are. -For JavaScript, in the first the this-object is *obj* while in the +For JavaScript, in the first the ``this``-object is ``obj`` while in the second it is the global-object. For explanation see [link]. Explicit this-call ^^^^^^^^^^^^^^^^^^ -Suppose *obj* is a JavaScript object. Then +Suppose ``obj`` is a JavaScript object. Then .. code-block:: javascript @@ -151,13 +151,13 @@ x = my_fn(a, b, c); -except that the this-object is set to *obj* (rather than the global +except that the ``this``-object is set to ``obj`` (rather than the global object). For more see [link]. Explicit this-apply ^^^^^^^^^^^^^^^^^^^ -Suppose *obj* is a JavaScript object. Then +Suppose ``obj`` is a JavaScript object. Then .. code-block:: javascript @@ -169,17 +169,17 @@ x = my_fn(a, b, c); -except that the this-object is set to *obj*. For more see [link]. +except that the ``this``-object is set to ``obj``. For more see [link]. Explicit this gotcha ^^^^^^^^^^^^^^^^^^^^ -If *obj* is not a JavaScript object then in +If ``obj`` is not a JavaScript object then in .. code-block:: javascript x = my_fn.call(obj, a, b, c); x = my_fn.apply(obj, [a, b, c]); -the this-object is set to the global object (which is probably not +the ``this``-object is set to the global object (which is probably not what you want). For more see [link].
--- a/source/index.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/index.rst Fri May 18 16:56:15 2012 +0200 @@ -2,7 +2,7 @@ ================================= -.. Tutorial: +Tutorial: .. toctree:: :maxdepth: 2 @@ -12,7 +12,6 @@ gotcha-quiz gotcha-answers - easiest-quiz-answers counters-example counters-discuss @@ -32,28 +31,6 @@ functions-and-this classes -.. ReferenceStubs:: - -.. AAAtoctree:: - :maxdepth: 2 - - functions-and-scope - bound-methods - this-and-that - call-and-apply - modules - global-object - strings - numbers - arrays - semicolons - braces - pseudo-objects - garbage-collection - glossary - delegation - dont-use-new - Resources: .. toctree::
--- a/source/javascript-objects.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/javascript-objects.rst Fri May 18 16:56:15 2012 +0200 @@ -5,7 +5,7 @@ ------------------- In JavaScript all objects are part of an inheritance tree. The -**create** function adds a node to the inheritance tree. +``create`` function adds a node to the inheritance tree. .. code-block:: javascript @@ -45,7 +45,7 @@ class explanation ^^^^^^^^^^^^^^^^^ -In Python we can subclass anything whose type is **type** (or a +In Python we can subclass anything whose type is ``type`` (or a subclass of type). A subclass (and its instances) inherits properties from the super-class. @@ -75,7 +75,7 @@ js> a['1'] 6 -In Python we can defined our own item methods. (The programmer owns +In Python we can define our own item methods. (The programmer owns the dot.) .. doctest:: @@ -101,7 +101,7 @@ >>> a['1'] 6 -Because **type(a)** is **A**, which has the special item methods, we +Because ``type(a)`` is ``A``, which has the special item methods, we get the special item behaviour. .. doctest:: @@ -113,7 +113,7 @@ On metaclass ------------ -Using previous definition, we cannot subclass **a** to create **b**. +Using previous definition, we cannot subclass ``a`` to create ``b``. .. doctest:: @@ -123,7 +123,7 @@ TypeError: Error when calling the metaclass bases object.__new__() takes no parameters -This is because **a** is not a type. The solution involves Python +This is because ``a`` is not a type. The solution involves Python metaclasses (an advanced topic). .. doctest:: @@ -157,7 +157,7 @@ Here is a more direct (and equivalent) construction (create an -instance of **ObjectType**, whose instances are objects). +instance of ``ObjectType``, whose instances are objects). .. doctest:: @@ -190,14 +190,14 @@ metaclass explanation ^^^^^^^^^^^^^^^^^^^^^ -Because **type(root)** is a subclass of type we can subclass root. +Because ``type(root)`` is a subclass of type we can subclass root. .. doctest:: >>> issubclass(type(root), type) True -Because the **type(root)** is **ObjectType**, which has special item +Because the ``type(root)`` is ``ObjectType``, which has special item methods, we get the special item behaviour. .. doctest:: @@ -210,7 +210,7 @@ ------------------ We can't call JavaScript objects (unless they are a function). But -**create** creates ordinary JavaScript objects. +``create`` creates ordinary JavaScript objects. .. code-block:: javascript
--- a/source/know-in-python.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/know-in-python.rst Fri May 18 16:56:15 2012 +0200 @@ -47,7 +47,8 @@ (when you are at the module scope, it's equal to the *global* scope). You don't need to do anything special to make that happen. -In JavaScript, variables go to the global object by default. You have to use the *var* keyword to have them defined locally. +In JavaScript, variables go to the global object by default. You have to use +the ``var`` keyword to have them defined locally. Functions passed as parameters @@ -56,7 +57,7 @@ Everything is an object in Python. In particular, a function is an object. This means that you can assign a function to a variable or pass it as a function argument. This may be useful when you want to have some generic algorithms. -This is used in the *sort* method so that you can, for example, sort strings in +This is used in the ``sort`` method so that you can, for example, sort strings in case-insensitive way: .. code-block:: python @@ -102,7 +103,7 @@ closure. An example use of a closure is a "private" variable that can be only accessed -by *get* and *set* functions. +by ``get`` and ``set`` functions. .. code-block:: python @@ -122,8 +123,8 @@ return get, set -The *value* variable stays in the memory even after *create_get_set* finishes, -because the *get* and *set* functions still hold a reference to it. Because +The ``value`` variable stays in the memory even after ``create_get_set`` finishes, +because the ``get`` and ``set`` functions still hold a reference to it. Because they are the only objects holding reference to it, the variable is effectively hidden from other code. You could have a similar effect by using an object: @@ -149,8 +150,8 @@ return getset.get, getset.set -Here instead of a closure we have an explicit *getset* object. Again, this -object stays in memory because the bound methods *get* and *set* hold a +Here instead of a closure we have an explicit ``getset`` object. Again, this +object stays in memory because the bound methods ``get`` and ``set`` hold a reference to it. This couldn't be done in JavaScript.
--- a/source/objects.rst Thu May 17 12:50:41 2012 +0200 +++ b/source/objects.rst Fri May 18 16:56:15 2012 +0200 @@ -3,7 +3,7 @@ JavaScript and Python have rather different object models and here we give a broad view of the differences. The simplest way to create a -JavaScript object **jso** is to write: +JavaScript object ``jso`` is to write: .. code-block:: javascript @@ -47,7 +47,7 @@ apple -Here's the corresponding Python session (with *sys.ps1 = 'py> '*): +Here's the corresponding Python session (with *sys.ps1='py> '*): .. code-block:: python @@ -56,9 +56,9 @@ py> pyo['A'] 'apple' -In the examples above *A* is the **key** and **apple** is the value. +In the examples above ``'A'`` is the key and ``'apple'`` is the value. In both cases we are able to store values against keys. In both cases -the value can be any object. In most other respects, jso and pyo are +the value can be any object. In most other respects, ``jso`` and ``pyo`` are different. Differences @@ -88,9 +88,9 @@ apple -What's going on here? Well, objects *a* and *b* are converted to -strings, and they have the same string representation, namely *[object -Object]*. (We'll see later how we can change this.) So let's try +What's going on here? Well, objects ``a`` and ``b`` are converted to +strings, and they have the same string representation, namely *'[object +Object]'*. (We'll see later how we can change this.) So let's try this out (continuing the previous example): .. code-block:: javascript @@ -134,7 +134,7 @@ Missing keys ^^^^^^^^^^^^ -The notation *a[b]* is called **indexing**. In Python, indexing with +The notation ``a[b]`` is called **indexing**. In Python, indexing with a key that is not present in the dictionary raises an exception. .. code-block:: python @@ -174,7 +174,7 @@ obj_a.method(args) obj_b.name -where respectively *obj_a.method* and *obj_name* are undefined. +where respectively ``obj_a.method`` and ``obj_name`` are undefined. Attributes @@ -201,7 +201,7 @@ .. note:: In JavaScript the three statements below are completely equivalent - (except for assignment to *tmp* as a side effect). + (except for assignment to ``tmp`` as a side effect). .. code-block:: javascript @@ -209,7 +209,7 @@ x = a['b'] tmp = 'b'; x = a[tmp] - In addition, when *a.b* is a function then + In addition, when ``a.b`` is a function then .. code-block:: javascript
