annotate source/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 4e6cfa066d1a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
5a1388541f50 index.rst: Added links to some stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
1 Classes
5a1388541f50 index.rst: Added links to some stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
2 =======
5a1388541f50 index.rst: Added links to some stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
3
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
4 In Python, *class* is a language primitive. JavaScript has no
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
5 built-in concept of class. Therefore, if we are to have classes in
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
6 JavaScript then we (or some library) must implement them. Here we
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
7 give an outline implementation of classes in JavaScript, so that the
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
8 basic language features can be understood.
6
5a1388541f50 index.rst: Added links to some stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
9
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
10 :ref:`Recall <inheritance-tree>` that each object has a parent, which
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
11 in turn has a parent, all the way up to the root object (which has no
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
12 parent). In Python (and other languages), classes are a way for
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
13 instances to share data and methods. As a first approximation, let us
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
14 say *two objects belong to the same class if they have the same
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
15 parent*.
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
16
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
17 As well as sharing class data, each instance of a class has its own
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
18 data, which is added to the instance as part of its creation. In
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
19 Python, this is usually done using the ``__init__`` method (although
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
20 sometimes ``__new__`` is also used).
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
21
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
22 Point in Python
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
23 ---------------
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
24
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
25 In Python we might write:
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
26
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
27 .. code-block:: python
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
28
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
29 class Point(object):
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
30
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
31 def __init__(self, x, y):
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
32 self.x = x
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
33 self.y = y
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
34
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
35 def move(self, dx, dy):
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
36 '''Move point by changes dx and dy.'''
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
37 self.x += dx
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
38 self.y += dy
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
39
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
40 mypoint = Point(2, 3)
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
41 mypoint.move(1, 1)
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
42
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
43
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
44 Point in JavaScript
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
45 -------------------
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
46
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
47 To implement a similar class in JavaScript we need a function Point
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
48 that returns a point, and an object that is a parent for all the
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
49 points created by the ``Point`` function. In Python we had a single
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
50 object, namely the class ``Point``, that performed both roles. In
68
ed7dd56152f7 classes.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
51 JavaScript we need a constructor function and a parent object.
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
52
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
53
68
ed7dd56152f7 classes.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
54 Parent object
ed7dd56152f7 classes.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
55 ^^^^^^^^^^^^^
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
56
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
57 We'll start with the parent object:
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
58
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
59 .. code-block:: javascript
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
60
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
61 var base = {}; // Grandparent for all instances.
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
62 var point = create(base); // Parent to all points.
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
63
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
64 point.__init__ = function(x, y){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
65 this.x = x;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
66 this.y = y;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
67 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
68
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
69 point.move = function(dx, dy){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
70 this.x += dx;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
71 this.y += dy;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
72 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
73
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
74 See [link] for why we use ``this`` rather than ``self``.
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
75
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
76
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
77 Constructor
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
78 ^^^^^^^^^^^
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
79
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
80 We also need a constructor function
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
81
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
82 .. code-block:: javascript
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
83
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
84 var Point = function(x, y){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
85
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
86 var instance = create(point);
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
87 instance.__init__(x, y);
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
88 return instance;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
89 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
90
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
91
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
92 In production we would use ``arguments`` and ``apply`` rather than
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
93 directly calling ``__init__``. Do you know why?
68
ed7dd56152f7 classes.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
94
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
95 Advanced features
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
96 -----------------
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
97
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
98 This can be omitted at a first reading.
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
99
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
100 Improved ``toString``
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
101 ^^^^^^^^^^^^^^^^^
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
102
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
103 In Python we have, for example:
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
104
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
105 .. code-block:: python
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
106
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
107 py> from point import Point
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
108 py> Point(2, 3)
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
109 <point.Point object at 0x20ead90>
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
110
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
111 We can get something similar in JavaScript by writing:
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
112
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
113 .. code-block:: javascript
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
114
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
115 point.__classname__ = 'Point';
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
116 base.toString = function(){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
117 return '<' + this.__classname__ + ' object>'
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
118 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
119
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
120 Constructor factory
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
121 ^^^^^^^^^^^^^^^^^^^
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
122
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
123 Here is a factory function for producing constructor functions,
211
4e6cfa066d1a Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents: 68
diff changeset
124 such as ``Point`` above.
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
125
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
126 .. code-block:: javascript
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
127
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
128 var constructor_factory(parent){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
129 return function(){
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
130 var instance = create(parent);
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 8
diff changeset
131 instance.__init__.apply(instance, arguments);
8
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
132 return instance;
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
133 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
134 };
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
135
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
136 var Point = constructor_factory(point);
62b9c15e287f classes.rst: Finished first draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
137