forked from WebProgrammingII/WEPO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_examples.js
57 lines (30 loc) · 1.47 KB
/
function_examples.js
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
/**
Function examples
*/
/* Part I - Basic functions */
// 1. Define a named function
// 2. Define a function as a variable
// 3. Define an anonymous function
// 4. Define a fat-arrow function
/* Part II - Hoisting */
// 1. Show how hoisting works within a function scope
/* Part III - Nested */
// 1. Create a nested function which uses a parameter from the outer function in it's calculations
/* Part IV - Function invocation */
// 1. Immediate invoke
// 3. As method invoke
// 4. As constructor invoke
// 5. Indirect through apply() or call()
// 5.1. Create a constructor function MobilePhone() which takes in manufacturer and size in GB
// 5.1.1. Create a function called changeProps()
// 5.2. Invoke changeProps() with apply()
// 5.3. Invoke changeProps() with call()
// 5.4. Assign changeProps() to a variable bound with bind() and call it
/* Part V - Parameters */
// 1. Create the method sumAll() which takes in x many arguments and returns the sum (using arguments)
// 2. Show how objects can be useful as parameters - e.g. given user print out namecard
// 3. Assign a "static" variable to the function which can be used to get the next number
/* Part VI - Namespaces */
// 1. Define a function as namespace and show how variables outside cannot be accessed
/* Part VII - Closures */
// 1. Create a closure by providing a function which given a number adds it to the number in local scope