Mercurial > js4py
annotate core-javascript/simple-classes.rst @ 213:0ea90a1819c2 default tip
Custom theming
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Wed, 27 Jun 2012 16:35:54 +0200 |
| parents | 29c7fe19aeb5 |
| 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 | |
|
207
29c7fe19aeb5
Mark inline code using duble backticks
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
120
diff
changeset
|
10 programming. The easiest way to do this is via ``create``. |
| 119 | 11 |
| 12 | |
| 13 Definition | |
| 14 ---------- | |
| 15 | |
| 16 Here's a factory function that creates classes. It returns a function | |
|
207
29c7fe19aeb5
Mark inline code using duble backticks
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
120
diff
changeset
|
17 called ``cls``, which in turn when called returns instances. At it's |
|
29c7fe19aeb5
Mark inline code using duble backticks
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
120
diff
changeset
|
18 heart is ``create``, and also ``apply`` and ``arguments``, which we've not |
| 119 | 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 |
|
207
29c7fe19aeb5
Mark inline code using duble backticks
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
120
diff
changeset
|
40 Here's an example of the use of ``SimpleClass``. There's a bug in it, |
|
120
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 |
