Mercurial > js4py-pl
annotate core-javascript/simple-classes.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 | fef162a89a47 |
| children |
| rev | line source |
|---|---|
| 118 | 1 Simple classes |
| 2 ============== | |
| 119 | 3 |
| 4 JavaScript has objects, which through the object tree support | |
| 5 inheritance. Almost all object-oriented languages have classes and | |
| 6 instances, but JavaScript has neither. This will be explained later, | |
| 7 along with a discussion of the consequences. | |
| 8 | |
| 9 For now our focus is on getting going with object-oriented | |
| 10 programming. The easiest way to do this is via create. | |
| 11 | |
| 12 | |
| 13 Definition | |
| 14 ---------- | |
| 15 | |
| 16 Here's a factory function that creates classes. It returns a function | |
| 17 called *cls*, which in turn when called returns instances. At it's | |
| 18 heart is *create*, and also *apply* and *arguments*, which we've not | |
| 19 seen before). | |
| 20 | |
| 21 .. literalinclude:: /core-javascript-work/library.js | |
| 22 :language: javascript | |
| 23 :start-after: start SimpleClass | |
| 24 :end-before: end SimpleClass | |
| 25 | |
| 26 | |
| 27 Why *cls*? | |
| 28 ---------- | |
| 29 | |
| 30 We call the returned function *cls* because in JavaScript *class*, | |
| 31 like *function* is a reserved word in JavaScript. However, in | |
| 32 JavaScript the identifier *class* has no meaning, and cannot be used | |
| 33 in any valid JavaScript program. It seems that they intended to | |
| 34 provide a built-in class capability, but never got round to it. | |
|
120
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
35 |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
36 |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
37 Fruit example |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
38 ------------- |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
39 |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
40 Here's an example of the use of *SimpleClass*. There's a bug in it, |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
41 and I've changed the test so it passes. Can you find the bug and fix |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
42 the example? |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
43 |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
44 .. literalinclude:: /core-javascript-work/test_library.js |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
45 :language: javascript |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
46 :start-after: start SimpleClass |
|
fef162a89a47
Add test_library.js (and fruit example).
Jonathan Fine <jfine@pytex.org>
parents:
119
diff
changeset
|
47 :end-before: end SimpleClass |
