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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
118
ccd7dbe83849 Add definition of SimpleClass.
jf3243
parents:
diff changeset
1 Simple classes
ccd7dbe83849 Add definition of SimpleClass.
jf3243
parents:
diff changeset
2 ==============
119
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
3
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
4 JavaScript has objects, which through the object tree support
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
5 inheritance. Almost all object-oriented languages have classes and
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
6 instances, but JavaScript has neither. This will be explained later,
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
7 along with a discussion of the consequences.
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
8
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
9 For now our focus is on getting going with object-oriented
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
10 programming. The easiest way to do this is via create.
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
11
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
12
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
13 Definition
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
14 ----------
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
15
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
16 Here's a factory function that creates classes. It returns a function
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
17 called *cls*, which in turn when called returns instances. At it's
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
18 heart is *create*, and also *apply* and *arguments*, which we've not
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
19 seen before).
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
20
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
21 .. literalinclude:: /core-javascript-work/library.js
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
22 :language: javascript
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
23 :start-after: start SimpleClass
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
24 :end-before: end SimpleClass
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
25
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
26
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
27 Why *cls*?
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
28 ----------
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
29
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
30 We call the returned function *cls* because in JavaScript *class*,
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
31 like *function* is a reserved word in JavaScript. However, in
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
32 JavaScript the identifier *class* has no meaning, and cannot be used
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
33 in any valid JavaScript program. It seems that they intended to
741a7969af83 Document SimpleClass (in the rst file).
jf3243
parents: 118
diff changeset
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