Mercurial > js4py
comparison source/javascript-objects.rst @ 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 | 454054afcc0a |
| children |
comparison
equal
deleted
inserted
replaced
| 210:622b8eec7dd6 | 211:4e6cfa066d1a |
|---|---|
| 3 | 3 |
| 4 Like Python classes | 4 Like Python classes |
| 5 ------------------- | 5 ------------------- |
| 6 | 6 |
| 7 In JavaScript all objects are part of an inheritance tree. The | 7 In JavaScript all objects are part of an inheritance tree. The |
| 8 **create** function adds a node to the inheritance tree. | 8 ``create`` function adds a node to the inheritance tree. |
| 9 | 9 |
| 10 .. code-block:: javascript | 10 .. code-block:: javascript |
| 11 | 11 |
| 12 // A JavaScript object. | 12 // A JavaScript object. |
| 13 js> root = {} | 13 js> root = {} |
| 43 5 | 43 5 |
| 44 | 44 |
| 45 class explanation | 45 class explanation |
| 46 ^^^^^^^^^^^^^^^^^ | 46 ^^^^^^^^^^^^^^^^^ |
| 47 | 47 |
| 48 In Python we can subclass anything whose type is **type** (or a | 48 In Python we can subclass anything whose type is ``type`` (or a |
| 49 subclass of type). A subclass (and its instances) inherits properties | 49 subclass of type). A subclass (and its instances) inherits properties |
| 50 from the super-class. | 50 from the super-class. |
| 51 | 51 |
| 52 >>> type(root) == type(a) == type(b) == type | 52 >>> type(root) == type(a) == type(b) == type |
| 53 True | 53 True |
| 73 | 73 |
| 74 js> a[1] = 6 | 74 js> a[1] = 6 |
| 75 js> a['1'] | 75 js> a['1'] |
| 76 6 | 76 6 |
| 77 | 77 |
| 78 In Python we can defined our own item methods. (The programmer owns | 78 In Python we can define our own item methods. (The programmer owns |
| 79 the dot.) | 79 the dot.) |
| 80 | 80 |
| 81 .. doctest:: | 81 .. doctest:: |
| 82 | 82 |
| 83 >>> class A(object): | 83 >>> class A(object): |
| 99 | 99 |
| 100 >>> a[1] = 6 | 100 >>> a[1] = 6 |
| 101 >>> a['1'] | 101 >>> a['1'] |
| 102 6 | 102 6 |
| 103 | 103 |
| 104 Because **type(a)** is **A**, which has the special item methods, we | 104 Because ``type(a)`` is ``A``, which has the special item methods, we |
| 105 get the special item behaviour. | 105 get the special item behaviour. |
| 106 | 106 |
| 107 .. doctest:: | 107 .. doctest:: |
| 108 | 108 |
| 109 >>> type(a) is A | 109 >>> type(a) is A |
| 111 | 111 |
| 112 | 112 |
| 113 On metaclass | 113 On metaclass |
| 114 ------------ | 114 ------------ |
| 115 | 115 |
| 116 Using previous definition, we cannot subclass **a** to create **b**. | 116 Using previous definition, we cannot subclass ``a`` to create ``b``. |
| 117 | 117 |
| 118 .. doctest:: | 118 .. doctest:: |
| 119 | 119 |
| 120 >>> class b(a): pass | 120 >>> class b(a): pass |
| 121 Traceback (most recent call last): | 121 Traceback (most recent call last): |
| 122 class b(a): pass | 122 class b(a): pass |
| 123 TypeError: Error when calling the metaclass bases | 123 TypeError: Error when calling the metaclass bases |
| 124 object.__new__() takes no parameters | 124 object.__new__() takes no parameters |
| 125 | 125 |
| 126 This is because **a** is not a type. The solution involves Python | 126 This is because ``a`` is not a type. The solution involves Python |
| 127 metaclasses (an advanced topic). | 127 metaclasses (an advanced topic). |
| 128 | 128 |
| 129 .. doctest:: | 129 .. doctest:: |
| 130 | 130 |
| 131 >>> isinstance(a, type) | 131 >>> isinstance(a, type) |
| 155 >>> class root(object): | 155 >>> class root(object): |
| 156 ... __metaclass__ = ObjectType | 156 ... __metaclass__ = ObjectType |
| 157 | 157 |
| 158 | 158 |
| 159 Here is a more direct (and equivalent) construction (create an | 159 Here is a more direct (and equivalent) construction (create an |
| 160 instance of **ObjectType**, whose instances are objects). | 160 instance of ``ObjectType``, whose instances are objects). |
| 161 | 161 |
| 162 .. doctest:: | 162 .. doctest:: |
| 163 | 163 |
| 164 >>> root = ObjectType('root', (object,), {}) | 164 >>> root = ObjectType('root', (object,), {}) |
| 165 >>> isinstance(root(), object) | 165 >>> isinstance(root(), object) |
| 188 | 188 |
| 189 | 189 |
| 190 metaclass explanation | 190 metaclass explanation |
| 191 ^^^^^^^^^^^^^^^^^^^^^ | 191 ^^^^^^^^^^^^^^^^^^^^^ |
| 192 | 192 |
| 193 Because **type(root)** is a subclass of type we can subclass root. | 193 Because ``type(root)`` is a subclass of type we can subclass root. |
| 194 | 194 |
| 195 .. doctest:: | 195 .. doctest:: |
| 196 | 196 |
| 197 >>> issubclass(type(root), type) | 197 >>> issubclass(type(root), type) |
| 198 True | 198 True |
| 199 | 199 |
| 200 Because the **type(root)** is **ObjectType**, which has special item | 200 Because the ``type(root)`` is ``ObjectType``, which has special item |
| 201 methods, we get the special item behaviour. | 201 methods, we get the special item behaviour. |
| 202 | 202 |
| 203 .. doctest:: | 203 .. doctest:: |
| 204 | 204 |
| 205 >>> type(root) == type(a) == type(b) == ObjectType | 205 >>> type(root) == type(a) == type(b) == ObjectType |
| 208 | 208 |
| 209 Never instantiated | 209 Never instantiated |
| 210 ------------------ | 210 ------------------ |
| 211 | 211 |
| 212 We can't call JavaScript objects (unless they are a function). But | 212 We can't call JavaScript objects (unless they are a function). But |
| 213 **create** creates ordinary JavaScript objects. | 213 ``create`` creates ordinary JavaScript objects. |
| 214 | 214 |
| 215 .. code-block:: javascript | 215 .. code-block:: javascript |
| 216 | 216 |
| 217 js> a = create(root) | 217 js> a = create(root) |
| 218 js> a(1, 2, 3) | 218 js> a(1, 2, 3) |
