-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_function_prototype_call.html
More file actions
408 lines (353 loc) · 18.1 KB
/
Copy pathjs_function_prototype_call.html
File metadata and controls
408 lines (353 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html lang="en">
<head>
<title>call() || JS CodeyLuwak</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="./img/keyboard-5-16.ico">
<link rel="stylesheet" href="./style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Barlow:ital,wght@0,400;0,700;1,400;1,700&family=Fira+Mono:wght@400;500;700&family=Roboto:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
</head>
<body>
<header>
<a href="./index.html"><h1>JavaScript: call()</h1></a>
<nav id="navMenu"></nav>
</header>
<main class="js-notes">
<article id="call-method">
<h2>Function.prototype.call()</h2>
<dl>
<dt><code>call()</code></dt>
<dd>
<code class="formula-thin">function <span class="variable">func</span>(<span class="variable">args</span>) {<span class="variable">...</span>}</code>
</dd>
<dd>
<code class="formula"><span class="variable">func</span>.call(<span class="variable">obj</span>, <span class="variable">args</span>)</code>
</dd>
<dd><code>call()</code> allows an <b>object's function</b> to be called for a <b>different object</b>. It provides a new value <code>this</code> to the function.</dd>
<dd>Or it can be a <b>generic object function</b> altogether, created independent of any existing object, but for object use. In this case, the <code>this</code> keyword inside the function will refert to the <code>global</code> if it's not in strict mode.</dd>
<dt>Syntax</dt>
<dd>
<pre class="formula">
call()
call(<span class="variable">obj</span>)
call(<span class="variable">obj</span>, <span class="variable">args</span>)
</pre>
</dd>
<dd>Parameters
<ul>
<li><code>obj</code>: The object the function is being called to. Refered as <code>this</code> from inside the function.</li>
<li><code>args</code>: comma separated arguments for the function</li>
</ul>
</dd>
<dd>Return value:
<ul>
<li>The result of calling the function with the specified <code>this</code> value and arguments.</li>
</ul>
</dd>
<dt><code>call()</code> vs <code>apply()</code></dt>
<dd><code>call()</code><br>
<code class="formula"><span class="variable">func</span>.call(<span class="variable">obj</span>, <span class="variable">arg1, arg2, ..., argN</span>)</code>
</dd>
<dd><code>apply()</code><br>
<code class="formula"><span class="variable">func</span>.apply(<span class="variable">obj</span>, [<span class="variable">arg1, arg2, ..., argN</span>])</code>
</dd>
<dd>Similarities:
<ul>
<li>Both are for calling a function to operate on an object.</li>
</ul>
</dd>
<dd>Differences:
<ul>
<li><code>apply()</code>: Function's arguments are given as an array.</li>
<li><code>call()</code>: Function's arguments are given as a comma separated list.</li>
</ul>
</dd>
</dl>
<a href="#top"><hr></a>
</article>
<article id="chaining-constructor">
<h2>Using call() to chain constructors</h2>
<dl>
<dt>Chaining constructors</dt>
<dd>Task: Chaining generic <code>Product</code> constructor with specialized constructors <code>Food</code> and <code>Toy</code>.</dd>
<dt>Generic <code>Product</code> object constructor</dt>
<dd>
<ul>
<li>Defined with 2 parameters: <code>name</code> and <code>price</code>.</li>
</ul>
</dd>
<dd>
<pre class="formula-thin">
function <span class="type">Product</span>(<span class="variable">name, price</span>) {
<b>this</b>.<span class="variable">name</span> = <span class="variable">name</span>;
<b>this</b>.<span class="variable">price</span> = <span class="variable">price</span>;
}
</pre>
</dd>
<dt>Specialized <code>Food</code> and <code>Toy</code> object constructors</dt>
<dd>
<ul>
<li>Invoke Product using <code>call()</code>, passing <code>this</code>, <code>name</code>, and <code>price</code>.</li>
<li><code>Product</code> initializes the properties <code>name</code> and <code>price</code>, then the specialized constructor define the <code>category</code>.</li>
</ul>
</dd>
<dd>
<pre class="formula-thin">
function <span class="type">Food</span>(<span class="variable">name, price</span>) {
<span class="formula-thin"><span class="type">Product</span>.call(<span class="type">this</span>, <span class="variable">name, price</span>);</span>
<b>this</b>.<span class="variable">categeory</span> = <span class="variable">"food"</span>;
}
function <span class="type">Toy</span>(<span class="variable">name, price</span>) {
<span class="type">Product</span>.call(<span class="type">this</span>, <span class="variable">name, price</span>);
<b>this</b>.<span class="variable">categeory</span> = <span class="variable">"toy"</span>;
}
</pre>
</dd>
<dt>Create objects with <code>new</code> keyword</dt>
<dd>
<pre class="formula-thin">
const <span class="variable">cheese</span> = new <span class="type">Food</span>(<span class="variable">"feta", 5</span>);
const <span class="variable">bread</span> = new <span class="type">Food</span>(<span class="variable">"muffin", 3</span>);
const <span class="variable">fun</span> = new <span class="type">Toy</span>(<span class="variable">"robot", 12</span>);
const <span class="variable">ocean</span> = new <span class="type">Toy</span>(<span class="variable">"figurine", 2</span>);
</pre>
</dd>
<dt>Results</dt>
<dd>
<pre class="formula-thin">
<span class="variable">cheese</span>; // <span class="type">Food</span> {<span class="variable">name: 'feta', price: 5, category: 'food'</span>}
<span class="variable">bread</span>; // <span class="type">Food</span> {<span class="variable"><span class="variable"><span class="variable">name: "muffin", price: 3, category: 'food'</span></span></span>}
<span class="variable">fun</span>; // <span class="type">Toy</span> {<span class="variable">name: 'robot', price: 12, category: 'toy'</span>}
<span class="variable">ocean</span>; // <span class="type">Toy</span> {<span class="variable">name: 'figurine', price: 2, category: 'toy'</span>}
</pre>
</dd>
</dl>
<a href="#top"><hr></a>
</article>
<article id="anonymous-function-and-direct-call">
<h2>Anonymous function and direct call()</h2>
<dl>
<dt>Anonymous function and direct calling</dt>
<dd><code>call()</code> can be used to invoke anonymous function directly after stating it.</dd>
<dd>
<pre class="formula">
<span class="variable">(<b>function</b>() {...})</span>.<b>call</b>(<span class="variable">thisArg</span>)
</pre>
</dd>
<dt>For each items in an object array</dt>
<dd>
<pre class="formula">
for (<span class="variable">let i = 0; i < arr.length; i++</span>) {
<span <span class="variable">(<b>function</b>() {...})</span>.<b>call</b>(<span class="variable">arr[i]</span>)
};
</pre>
</dd>
<dd>Don't forget keyword <code>this</code></dd>
<dt>Task</dt>
<dd>Create anonymous function for printing content of each properties in an object array, with index numbering but starting from 1.</dd>
<dt>Basic way</dt>
<dd>
<pre class="formula-thin">
const <span class="variable">animals</span> = [
{<span class="variable">species: "Dodo", name: "Dottie"</span>},
{<span class="variable">species: "Flummel", name: "Op"</span>},
{<span class="variable">species: "Flummel", name: "Ed"</span>}
];
for (<span class="variable">let i = 0; i < animals.length; i++</span>) {
<span class="variable">console.log(`#${i+1} ${animals[i].species}: ${animals[i].name}`);</span>
};
<span class="comment">----------------
#1 Dodo: Dottie
#2 Flummel: Op
#3 Flummel: Ed
----------------</span>
</pre>
</dd>
<dt>Using function</dt>
<dd>
<pre class="formula-thin">
const <span class="variable">animals</span> = [
{<span class="variable">species: "Dodo", name: "Dottie"</span>},
{<span class="variable">species: "Flummel", name: "Op"</span>},
{<span class="variable">species: "Flummel", name: "Ed"</span>}
];
function print(arr, i) {
<span class="variable">console.log(`#${i+1} ${arr[i].species}: ${arr[i].name}`);</span>
}
for (<span class="variable">let i = 0; i < animals.length; i++</span>) {
print(animals, i)
};
<span class="comment">----------------
#1 Dodo: Dottie
#2 Flummel: Op
#3 Flummel: Ed
----------------</span>
</pre>
</dd>
<dt>Using anonymous function and direct <code>call()</code></dt>
<dd>Working from the <code>print</code> function above</dd>
<dd>
<ul>
<li>The <code>this</code> is <code>animals[i]</code></li>
<li>Then the call: <code>call(animals[i], i)</code></li>
<li>And inside the function: <code>this.species</code> and <code>this.name</code></li>
<li>Parameters <code>arr</code> and <code>i</code> are not necessary</li>
</ul>
</dd>
<dd>From this:</dd>
<dd>
<pre class="formula-thin">
const <span class="variable">animals</span> = [
{<span class="variable">species: "Dodo", name: "Dottie"</span>},
{<span class="variable">species: "Flummel", name: "Op"</span>},
{<span class="variable">species: "Flummel", name: "Ed"</span>}
];
for (<span class="variable">let i = 0; i < animals.length; i++</span>) {
(function (arr, i) {
<span class="variable">console.log(`#${i+1} ${arr[i].species}: ${arr[i].name}`);</span>
}).call(animals[i], animals, i)
};
<span class="comment">----------------
#1 Dodo: Dottie
#2 Flummel: Op
#3 Flummel: Ed
----------------</span>
</pre>
</dd>
<dd>To this:</dd>
<dd>
<pre class="formula-thin">
const <span class="variable">animals</span> = [
{<span class="variable">species: "Dodo", name: "Dottie"</span>},
{<span class="variable">species: "Flummel", name: "Op"</span>},
{<span class="variable">species: "Flummel", name: "Ed"</span>}
];
for (<span class="variable">let i = 0; i < animals.length; i++</span>) {
(function () {
<span class="variable">console.log(`#${i+1} ${this.species}: ${this.name}`);</span>
}).call(animals[i])
};
<span class="comment">----------------
#1 Dodo: Dottie
#2 Flummel: Op
#3 Flummel: Ed
----------------</span>
</pre>
</dd>
</dl>
<a href="#top"><hr></a>
</article>
<article id="invoke-function-containing-this">
<h2>Invoke object functions and give context to "this"</h2>
<dl>
<dt>Invoke object functions and give context to "this"</dt>
<dd>Functions can be made for use in object. Inside function, the object is refered as <code>this</code>. Function then could be invoke conveniently using <code>call(object)</code>.</dd>
<dd>
<pre class="formula">
function <span class="variable">func</span> () { <span class="variable">this.property...</span> }
const <span class="variable">obj</span> = { <span class="variable">property1: "value1", property2: "value2", ...</span> }
<span class="variable">func</span>.call(<span class="variable">obj</span>)
</pre>
</dd>
<dt>Example</dt>
<dd>
<pre class="formula-thin">
function <span class="variable">dimension</span> () {
<span class="variable">console.log(`The ${this.type} paper size is ${this.width} inch by ${this.length} inch.`)</span>;
};
const <span class="variable">letter</span> = {
<span class="variable">type: "letter",
width: 8.5,
length: 11</span>
};
<span class="variable">dimension</span>.call(<span class="variable">letter</span>);
<span class="comment">// The letter paper size is 8.5 inch by 11 inch.</span>
</pre>
</dd>
</dl>
<a href="#top"><hr></a>
</article>
<article id="blank-call-invokes-global-object">
<h2>Blank call() to invoke global value</h2>
<dl>
<dt>Blank <code>call()</code> to invoke global value</dt>
<dd>When <code>thisArg</code> is not given, global value is used. Because without context, <code>this</code> referes to the global object.</dd>
<dd>We can set a global values as default <code>this.property</code> values. This will be used when the <code>thisArg</code> is not given as an argument when calling.</dd>
<pre class="formula">
function <span class="variable">func</span> () { <span class="variable">this.property...</span> }
const <span class="variable">obj</span> = { <span class="variable">property1: "value1", property2: "value2", ...</span> }
<span class="variable">func</span>.call(<span class="variable">obj</span>)
</pre>
</dd>
<dt>Task</dt>
<dd>Create <code>welcome</code> function that can send welcome message to name within an object, to all objects in an array (through a for function), and give default <code>Guest</code> for use when there's no specific object.</dd>
<dd>
<pre class="formula-thin">
var <span class="variable">name</span> = <span class="variable">"Guest"</span>;
function <span class="variable">welcome</span>() {
<span class="variable">console.log(`Welcome, ${this.name}!`);</span>
};
const <span class="variable">son</span> = { <span class="variable">name: "Dodo", age: 5</span> };
const <span class="variable">children</span> = [
{ <span class="variable">name: "Dottie", age: 5</span> },
{ <span class="variable">name: "Ed", age: 3</span> },
{ <span class="variable">name: "Op", age: 2</span> }
];
</pre>
</dd>
<dt>One function for all</dt>
<dd>Just the one function can suit default, single object, and array of objects:</dd>
<dd>
<pre class="formula-thin">
<span class="variable">welcome</span>.<b>call</b>();
<span class="comment">// Welcome, Guest!</span>
<span class="variable">welcome</span>.<b>call</b>(<span class="variable">son</span>);
<span class="comment">// Welcome, Dodo!</span>
<span class="variable">children</span>.<b>forEach</b>(<span class="variable">el</span> <b>=></b> <span class="variable">welcome</span>.<b>call</b>(<span class="variable">el</span>));<span class="comment">
// Welcome, Dottie!
// Welcome, Ed!
// Welcome, Op!</span>
</pre>
</dd>
<dd>Other outcomes:</dd>
<dd>
<pre class="formula-thin">
<span class="variable">welcome</span>();
<span class="variable">welcome</span>(<span class="variable">son</span>);
<span class="variable">welcome</span>(<span class="variable">children</span>);
<span class="comment">// Welcome, Guest!</span>
<span class="variable">welcome</span>.<b>call</b>(<span class="variable">children</span>);
<span class="comment">// Welcome, undefined!</span>
</pre>
</dd>
<dt>Doesn't work on strict mode <code>"use strict"</code></dt>
<dd>
<pre class="formula-thin">
<b>"use strict"</b>
var <span class="variable">name</span> = <span class="variable">"Guest"</span>;
function <span class="variable">welcome</span>() {
<span class="variable">console.log(`Welcome, ${this.name}!`);</span>
};
<span class="variable">welcome</span>();
<span class="variable">welcome</span>.call();
<span class="comment">// Uncaught TypeError...</span>
</pre>
</dl>
<a href="#top"><hr></a>
</article>
<article id="references">
<h2>References</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" target="_blank">MDN: Function.prototype.call()</a></li>
</ul>
</article>
</main>
<footer>
<div class="nav-bar"><a href="./index.html" class="nav-button-rev" style="margin: auto">Home</a></div>
</footer>
<script src="./navArticle.js"></script>
</body>
</html>