-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-js2.html
More file actions
152 lines (125 loc) · 3.41 KB
/
basic-js2.html
File metadata and controls
152 lines (125 loc) · 3.41 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
<!DOCTYPE html>
<html>
<body>
<h2>Immediately Invoked Function Expression
</h2>
<code>
var main = function() {
(function() {
for (var x = 0; x < 5; x++) {
console.log(x);
}
})();
console.log("x can not be accessed outside the block scope x value is :" + x);
}
main();
</code>
<script>
var main = function() {
(function() {
for (var x = 0; x < 5; x++) {
console.log(x);
}
})();
console.log("x can not be accessed outside the block scope x value is :" + x);
}
main();
</script>
<h2>Generator Functions
</h2>
<code>
"use strict"
function* rainbow() {
// the asterisk marks this as a generator
yield 'red';
yield 'orange';
yield 'yellow';
yield 'green';
yield 'blue';
yield 'indigo';
yield 'violet';
}
for (let color of rainbow()) {
console.log(color);
}
</code>
<script>
"use strict"
function* rainbow() {
// the asterisk marks this as a generator
yield 'red';
yield 'orange';
yield 'yellow';
yield 'green';
yield 'blue';
yield 'indigo';
yield 'violet';
}
for (let color of rainbow()) {
console.log(color);
}
</script>
<h2>The Object.assign() Function
</h2>
<script>
"use strict"
var det = {
name: "Tom",
ID: "E1001"
};
var copy = Object.assign({}, det);
// console.log(copy);
for (let val in copy) {
console.log(copy[val])
}
</script>
<h2>Invoked through call or apply</h2>
<script>
var adder = {
base: 1,
add: function(a) {
var f = v => v + this.base;
return f(a);
},
addThruCall: function(a) {
var f = v => v + this.base;
var b = {
base: 2
};
return f.call(b, a);
},
addMore: function(a) {
var f = v => v + this.base;
var b = {
base: 4
};
return f.call(b, a);
}
};
console.log("Invoked through call or apply");
console.log(adder.add(1)); // This would log 2
console.log(adder.addThruCall(1)); // This would log 2 still
console.log(adder.addMore(2));
</script>
<h2>
No binding of arguments
</h2>
<script>
var arguments = [1, 2, 3];
var arr = () => arguments[0];
arr(); // 1
function foo(n) {
var f = () => arguments[0] + n; // foo's implicit arguments binding. arguments[0] is n
return f();
}
function bar(n) {
var b = () => arguments[0] + n;
return b();
}
console.log("No binding of arguments ");
foo(3); // 6
console.log("foo" + foo(3));
console.log("BAR " + bar(2));
</script>
</body>
</html>