annotate source/functions.rst @ 162:77e87f1b5811 core-javascript

$ hg merge core-javascript.closure-exercises # done, at least for now
author jf3243
date Wed, 09 Nov 2011 17:00:38 +0000
parents b76dbf891f88
children 4e6cfa066d1a
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 Functions
5a1388541f50 index.rst: Added links to some stub pages.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
2 =========
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
3
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
4 A function contains code, possibly with parameters, that is stored for
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
5 later use. In both JavaScript and Python, functions are first-class
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
6 objects. This means that identifiers can have a function as their
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
7 value, and be used as the parameter or return value of a function.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
8
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
9 It's common, when writing JavaScript for web pages, to create a large
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
10 number of anonymous (nameless) and similar event-handing functions,
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
11 one for each node. Often, :doc:`delegation` allows us replace a many
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
12 similar functions by a single function.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
13
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
14 Defining a function
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
15 -------------------
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
16
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
17 When defining a function in JavaScript use
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
18
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
19 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
20
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
21 var my_fn = function(arg1, arg2, arg3){
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
22 // Body of the function.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
23 };
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
24
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
25 as the template, where of course you get to choose the number and
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
26 names of the parameters. Unlike Python, you can't provide default
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
27 values for the arguments, nor can you provide **\*args** and
66
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
28 **\*\*kwargs**. However, as we will see, there is the pseudo-variable
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
29 *arguments* in JavaScript.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
30
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
31 This template can be adapted
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
32
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
33 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
34
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
35 obj.my_fn = function(arg1, arg2, arg3){
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
36 // Body of the function.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
37 };
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
38
66
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
39 to define an attribute or method of an object. This is most commonly
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
40 done when *obj* is a prototype.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
41
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
42 You can also put a function in an object literal, as in
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
43
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
44 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
45
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
46 obj = {
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
47
66
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
48 'my_fn': function(arg1, arg2, arg3){
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
49 // Body of the function.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
50 }
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
51 };
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
52
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
53 but this is often best avoided.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
54
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
55 JavaScript provides another method (called function declaration)
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
56
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
57 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
58
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
59 function my_fn(arg1, arg2, arg3){
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
60 // Body of the function.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
61 };
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
62
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
63 which should (in the author's view) never be used. It has strange
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
64 properties [link].
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
65
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
66
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
67 .. _calling-a-function:
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
68
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
69 Calling a function
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
70 ------------------
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
71
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
72 There are four ways of calling a function in JavaScript. The
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
73 difference involves :doc:`functions-and-this`, which we've not covered
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
74 yet. In brief, the four forms are:
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
75
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
76 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
77
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
78 x = my_fn(a, b, c); // Simple function call.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
79 x = obj.my_fn(a, b, c); // Method call.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
80 x = my_fn.call(obj, a, b, c); // Explicit this call.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
81 x = my_fn.apply(obj, [a, b, c]); // Explicit this apply.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
82
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
83 For all forms of the function call, supplying the wrong number of
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
84 arguments **does not raise an error**. Instead, excess arguments are
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
85 ignored, and excess parameters are initialised to *undefined*.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
86
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
87
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
88 Function call
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
89 ^^^^^^^^^^^^^
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
90
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
91 This is the simplest form of function call.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
92
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
93 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
94
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
95 x = my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
96
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
97 The this-object is set to the :doc:`global-object`, which causes a
66
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
98 problem only if the body of the function refers to *this*. Many
b76dbf891f88 functions.rst: Minor editorial changes.
Jonathan Fine <jfine@pytex.org>
parents: 12
diff changeset
99 functions are intended to be used in this way.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
100
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
101
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
102 Method call
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
103 ^^^^^^^^^^^
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
104
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
105 This is the usual, but not the most general, way of setting the this-object.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
106
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
107 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
108
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
109 x = obj.my_fn(a, b, c); // Usual form.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
110 x = obj['my_fn'](a, b, c); // Variant form.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
111
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
112 The two forms are completely equivalent. The this-object is set to
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
113 the *obj*.
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
114
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
115 The two following calls are always equivalent:
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
116
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
117 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
118
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
119 x = a.b.c.d.e.my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
120 tmp = a.b.c.d.e
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
121 x = tmp.my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
122
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
123 Method call gotcha
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
124 ^^^^^^^^^^^^^^^^^^
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
125
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
126 In JavaScript the two following calls are rarely equivalent:
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
127
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
128 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
129
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
130 x = obj.my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
131 tmp = obj.my_fn
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
132 x = tmp(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
133
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
134 whereas in Python they always are.
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
135
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
136 For JavaScript, in the first the this-object is *obj* while in the
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
137 second it is the global-object. For explanation see [link].
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
138
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
139 Explicit this-call
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
140 ^^^^^^^^^^^^^^^^^^
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
141
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
142 Suppose *obj* is a JavaScript object. Then
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
143
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
144 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
145
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
146 x = my_fn.call(obj, a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
147
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
148 is equivalent to
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
149
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
150 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
151
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
152 x = my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
153
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
154 except that the this-object is set to *obj* (rather than the global
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
155 object). For more see [link].
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
156
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
157 Explicit this-apply
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
158 ^^^^^^^^^^^^^^^^^^^
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
159
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
160 Suppose *obj* is a JavaScript object. Then
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
161
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
162 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
163
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
164 x = my_fn.apply(obj, [a, b, c]);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
165
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
166 is equivalent to
11
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
167
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
168 .. code-block:: javascript
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
169
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
170 x = my_fn(a, b, c);
ede8f282fb9f functions.rst: Good enough for the current draft.
Jonathan Fine <jfine@pytex.org>
parents: 6
diff changeset
171
12
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
172 except that the this-object is set to *obj*. For more see [link].
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
173
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
174 Explicit this gotcha
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
175 ^^^^^^^^^^^^^^^^^^^^
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
176
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
177 If *obj* is not a JavaScript object then in
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
178
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
179 .. code-block:: javascript
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
180
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
181 x = my_fn.call(obj, a, b, c);
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
182 x = my_fn.apply(obj, [a, b, c]);
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
183
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
184 the this-object is set to the global object (which is probably not
4be9f1bf2fd3 Reviewed and revised existing pages, and added some new stub pages.
Jonathan Fine <jfine@pytex.org>
parents: 11
diff changeset
185 what you want). For more see [link].