Mercurial > js4py-pl
annotate core-javascript/object-tree.rst @ 201:b6864a86e876 default tip
Przetłumaczone getting-started
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sun, 15 Jul 2012 13:27:27 +0200 |
| parents | d123fcf749bc |
| children |
| rev | line source |
|---|---|
| 102 | 1 The object tree |
| 2 =============== | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
3 |
| 102 | 4 Objects can have attributes. The statements |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
5 |
| 102 | 6 .. code-block:: javascript |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
7 |
| 102 | 8 value = obj.attr # Get the attr of 'a'. |
| 9 obj.attr = value # Set the attr of 'a'. | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
10 |
| 102 | 11 respectively **set** and **get** the *attr* attribute of the object |
| 12 *obj*. Inheritance is where an object gets some its attributes from | |
| 13 one or other more objects. JavaScript uses an object tree for | |
| 14 inheritance. | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
15 |
|
8
62b9c15e287f
classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents:
5
diff
changeset
|
16 |
|
62b9c15e287f
classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents:
5
diff
changeset
|
17 .. _inheritance-tree: |
|
62b9c15e287f
classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents:
5
diff
changeset
|
18 |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
19 Tree |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
20 ---- |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
21 |
| 102 | 22 All JavaScript objects are part of an inheritance tree. Each object |
| 23 in the tree has a parent object, which is also called the prototype | |
| 24 object (of the child). There is a single exception to this rule, | |
| 25 which is the **root** of the tree. The root of the tree does not have | |
| 26 a parent object. | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
27 |
| 102 | 28 .. note:: |
| 29 | |
| 30 **You can't get far in JavaScript without understanding the | |
| 31 object tree.** | |
| 32 | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
33 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
34 Get |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
35 ^^^ |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
36 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
37 When JavaScript needs to get an attribute value of an object, it first |
| 102 | 38 looks up the attr of the attribute in the object's dictionary. If the |
| 39 attr is a key in the dictionary, the associated value is returned. | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
40 |
| 102 | 41 If the attr is not a key, then the process is repeated using the |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
42 object's parent, grandparent, and so on until the key is found. If |
| 102 | 43 the key is not found in this way then *undefined* is returned. |
| 44 | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
45 |
| 102 | 46 Set |
| 47 ^^^ | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
48 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
49 When JavaScript needs to set an attribute value of an object it |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
50 ignores the inheritance tree. It simply sets that value in the |
| 102 | 51 object's dictionary. |
| 52 | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
53 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
54 Root |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
55 ---- |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
56 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
57 When the interpreter starts up, the **root** of the tree is placed at |
| 102 | 58 *Object.prototype*. (We'll find out later why that location is used.) |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
59 |
|
12
4be9f1bf2fd3
Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
8
diff
changeset
|
60 Every object inherits from the **root**, although perhaps not |
| 102 | 61 directly. Here's a simple example: |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
62 |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
63 .. code-block:: javascript |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
64 |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
65 js> root = Object.prototype |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
66 js> a = {} |
| 102 | 67 js> a.attr === undefined |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
68 true |
| 102 | 69 js> root.attr = 'gotcha' |
| 70 js> a.attr | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
71 gotcha |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
72 |
| 102 | 73 If we give *root* a *attr* attribute then every other object, |
| 74 including those already created and those not yet created, also has a | |
| 75 *attr* attribute with the same value. (In practice it's better not to | |
| 76 change Object.prototpye.) | |
| 77 | |
| 78 This applies arrays: | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
79 |
| 102 | 80 .. code-block:: javascript |
| 81 | |
| 82 js> array = [0, 1, 2] | |
| 83 0,1,2 | |
| 84 js> array.attr | |
| 85 gotcha | |
| 86 | |
| 87 And even to functions: | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
88 |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
89 .. code-block:: javascript |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
90 |
| 102 | 91 js> f = function(){} |
| 92 function () { | |
| 93 } | |
| 94 | |
| 95 js> f.attr | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
96 gotcha |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
97 |
| 102 | 98 .. note:: |
| 99 | |
| 100 A page might have many scripts, all of which would like to modify | |
| 101 the Object.prototype root object. This can cause bugs and | |
| 102 incompatibilities. So try not to do this. | |
| 103 | |
| 104 | |
| 105 However, this up-the-tree lookup does not apply if *attr* is found | |
| 106 earlier in the tree. We continue the previous example to show this, | |
| 107 and the behaviour of set. | |
| 108 | |
| 109 .. code-block:: javascript | |
| 110 | |
| 111 js> a.attr = 'fixed' | |
| 112 js> a.attr | |
| 113 fixed | |
| 114 js> root.attr | |
| 115 gotcha | |
| 116 | |
| 117 | |
| 118 Create | |
| 119 ------ | |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
120 |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
121 Any tree can be constructed from its root, together with a command |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
122 **create(parent)** that returns a new **child** of the given parent |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
123 node. |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
124 |
| 117 | 125 In all but the most recent version of JavaScript the *create* function |
| 126 is not built in. However, it's easy to write one, once you know | |
| 127 enough JavaScript. Here's how its done in the work folder's | |
| 128 library.js file. | |
| 102 | 129 |
| 117 | 130 .. literalinclude:: /core-javascript-work/library.js |
| 131 :language: javascript | |
| 132 :start-after: start create | |
| 133 :end-before: end create | |
| 102 | 134 |
| 135 | |
| 136 Using create | |
| 137 ------------ | |
| 138 | |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
139 Here's an example of its use: |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
140 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
141 .. code-block:: javascript |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
142 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
143 js> a = {} |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
144 js> b = create(a) |
| 102 | 145 js> a.attr = 'apple' |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
146 apple |
| 102 | 147 js> b.attr |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
148 apple |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
149 |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
150 |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
151 And a continuation of the example: |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
152 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
153 .. code-block:: javascript |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
154 |
|
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
155 js> c = create(b) |
| 102 | 156 js> c.attr |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
157 apple |
| 102 | 158 js> b.attr = 'banana' |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
159 banana |
| 102 | 160 js> c.attr |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
161 banana |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
162 |
|
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
163 |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
164 .. note:: |
|
3
65872f01ea89
inheritance.rst: Made a start on this page.
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
165 |
|
5
48d5ba601286
inheritance.rst: Finished, at least for this draft.
Jonathan Fine <jfine@pytex.org>
parents:
3
diff
changeset
|
166 JavaScript uses an inheritance tree. By using *create*, we can |
|
12
4be9f1bf2fd3
Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
8
diff
changeset
|
167 create any inheritance tree. All JavaScript objects are in this |
|
4be9f1bf2fd3
Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
8
diff
changeset
|
168 tree. |
