-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.js
198 lines (160 loc) · 5.02 KB
/
sandbox.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
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
var flag = true //old
let flag1 = true
/* undefined is given by
browsers when the variable
is not defined - different from null
*/
const flag2 = null
// ------------------this is a function expression---------------
document.getElementById('button').onclick = function () {
if (flag) {
document.getElementById('content').innerHTML = 'hello'
flag = false
} else {
document.getElementById('content').innerHTML = ''
flag = true
}
};
// the ; at the end just terminates the assignment statement
document.getElementById('button2').onclick = function(){
let firstName = document.getElementById('input1').value;
let lastName = document.getElementById('input2').value;
let fullName = firstName +" "+ lastName;
document.getElementById('output').innerHTML = fullName;
}
// array methods
/* .join(char) - join the array and output a string
with the joining character is char
*/
// .indexOf('element')
// .push() | .pop()
// .concat(['aasf','b', 20])
/* .includes() - also a string method -
find the passed parameter in string or array
*/
/* template strings with `the age is ${age}` -
as python string formatting
*/
let age = 20
list = [age, age+3, `the age is ${age}`]
console.log(list)
// type conversion
// "100" + 100 - not allowed
let value = 200
console.log(Number('100')+ value) // works
console.log(typeof value)
/* labels - only search for lableName above
the break/continue labelName statement
*/
outerlabel: for (let index = 0; index < 10; index++) {
for (let index2 = 0; index2 < 10; index2++) {
console.log(`index1 - ${index}, index2 ${index2}`);
continue outerlabel;
}
}
// JS switch statements can be grouped and the checks are always strict
document.getElementById('browser').oninput = function () {
let browser = document.getElementById('browser').value
switch (browser) {
case 'Mozilla':
case 'Chrome':
case 'Safari':
case 'Edge':
document.getElementById('result').innerHTML = 'we support this browser';
break;
case 'Internet Explorer' :
document.getElementById('result').innerHTML = 'this browser is not supported';
break;
default:
document.getElementById('result').innerHTML = 'this browser is not supported';
break;
}
}
// alert(), prompt() and confirm()
/* alert - returns undefined |
prompt - returns a string of anything
that was typed in the prompt
*/
// if same variable is declared
// within function block and out of it
// the inner variable is used
// we can call funcs of func exprs as a
// parameter of another func - called callbacks
// declared funcs can be called before they are declared
// func exprs cant, but they can be used outside and a
// their blocks as they are variables
let callbacks = function (question, yes, no){
if(confirm(question)) yes()
else no()
}
let showTrue = function(){
document.getElementById('result-2').innerHTML = 'you confirmed yes'
}
let showFalse = function(){
document.getElementById('result-2').innerHTML = 'you cancelled'
}
// ======================================================================
// arrow functions - single line - dont have their own this, therefore better use in classes
// multi line with curly brackets
let name = n => n*2 //one argument n
let sayHi = () => 'hello' //no argument
let sayHello = (a,b) =>{ //with arguments
return 'hi'
}
console.log(name(2))
console.log(sayHi())
console.log(sayHello(10,20))
let multiply= () =>{
a = document.getElementById('in').value
b = document.getElementById('in-1').value
document.getElementById('result-3').innerHTML = a*b
}
// =================================================
// JS Closure
// =================================================
// js closure is when an inner function can acces its surrounding state
// or lexical scope even after the outer function has returned
// used to emulate private methods and variables (with IIFE - immediately invoked fun expr)
let outerFunc = function(){
let privateVar = 0
let privateMethod = (add) => privateVar += add;
return {
increment: function(){
privateMethod(1)
},
decrement: function(){
privateMethod(-1)
} ,
value : function (){return privateVar;}
}
};
counter = outerFunc(); //counter is now instance of outerFunc
// also
let addN = (a) => {
return function(b){
return a+b
}
}
let add5 = addN(5)
console.log(add5(2))
let add10 = addN(10)
console.log(add10(21))
// event loop resides between stack and the task queue
// and pushes any task in the task queue when the stack is empty
/*
__________ ____________
| | |callback/ |
| | <-event -> |task |
|Stack | loop |queue |
| | | |
| | | |
---------- ------------
__________
| |
|browser |
|web |
|api |
| |
----------
/*
// ** HTML elements can be referenced in JS by their id attrs. **