-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjs_cheatsheet.js
More file actions
369 lines (312 loc) · 8.28 KB
/
js_cheatsheet.js
File metadata and controls
369 lines (312 loc) · 8.28 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
// comment
/* multi-line comment */
console.log(`My name is ${name}.`);
// primitives: number, string, boolean, undefined, null, symbol
// falsy values: undefined, null, 0, '', NaN
typeof(var);
myList instanceof Array;
myList.constructor.name; //Array
isNaN(input);
// == : equality
// === : equality & type coersion
eval("4+5"); -> 9
// Variables (function scoped)
var firstName, lastName;
firstName = "Homer";
// unassigned variable == 'undefined' value/datatype
// absence of value == 'null' value/obj datatype
// Variables (block scoped, can't use before assigned)
let firstName = 'john'; //replaces var
const year = 1990;
let a = 1;
{
const a = 2; //OK: block scoped
}
// Strings
var str = "a " + "b";
str.length;
str.charAt(0);
str.toUpperCase();
str.substring(0,5);
str.replace('a', 'foo');
parseInt(str)
parseFloat(str)
str.startsWith('str');
str.includes('str');
str.repeat(numT);
str.split(" ");
for (const c of str) { };
[...str].forEach(c => );
// Arrays
var arr = new Array(3);
arr = [1, 2, 3];
arr[0] = "foo";
arr.length
arr.indexof("hello");
arr.slice(begin, end); // new copy portion of arr
arr.splice(begin, deleteCount, e); // remove/add elems from arr
arr.sort();
arr.reverse();
arr.includes(e);
arr.findIndex(e => );
arr.find(e => );
arr.join(' ');
arr = Array.from(iterable/str, e => );
arr.concat(arr2); // ret new arr
arr.push("world"); //add to end
arr.unshift("hello"); //add to begin
arr.pop(); // remove from end
arr.shift(); // remove from begin
for (var i = 0; i < arr.length; i++) { }
for (var e of arr) { }
arr.forEach(function(currEle, idx, arr) { } );
arr.forEach(e => { })
arr.filter(e => e.length > 2);
arr.map(function(currEle, idx, arr) {} );
arr.map(e => { });
arr.map((e, i) => { return e + i; })
arr.reduce((acc, cur) => {
return acc++;
}, start);
// Map (retains keys in insertion order)
let map = new Map();
map = new Map(Object.entries(obj));
map.set(k, v);
map.get(k);
map.has(k);
map.delete(k);
map.size;
map.keys();
map.entries();
map.clear();
for(let [k, v] of map) { };
map.forEach((v, k) => { } );
// Set
let set = new Set([1,2,3]);
set.add(e);
set.has(e);
set.delete(e):
set.size;
set.clear();
set.forEach(e => {});
// If/else
if (a > b) { }
else { }
// Switch
switch (color)
{
case "red":
break;
default:
break;
}
var age = 20
switch(true)
{
case age < 13:
break;
}
// Functions
// Is an object, with invocable code
// Function statement (doesn't return anything)
function foo(param1, param2) {
return param1 + param2;
}
// Function expression (returns created fn obj)
var foo = function(param1, param2) {
return param1 + param2;
}
// func only does one thing, and return. Can omit {} and 'return'
var foo = (param1, param2) => param1 + param2;
// IIFE (Immediately Invoked Function Expression)
// (used for data hiding (var safety) /modularization)
// () before FUNCTION keyword tricks parser into thinking it's an expression (not statement)
(function(param1, param2) {
return param1 + param2;
})(1, 2); // () in the end executes it
// Async function
const fn = id => {
return new Promise( (resolve, reject) => {
setTimeout( (idx) => {
resolve([idx,1,2,3]);
}, 1500, id);
});
};
fn().then(arr => {
return arr[0];
}).catch(error => {
console.log('err');
});
async function fn2() {
// blocks until resolved
const ids = await fn(1);
// returns another promise
return ids[0];
}
// Import/export modules
export default 'foo bar baz'; //1 (default exports)
export const add = (a,b) => a+b; //2 (named exports)
import anyVarName from './views/searchView'; //1
import { add as a, mult as b, ID} from './views/searchView'; //2
import * as searchView from './views/searchView'; //2
// Error handling
function foo() {
try {
bar();
} catch(e) {
alert("ERROR");
}
}
// Objects
// Object literal (anonymous)
var john = {
name: 'John',
greet: function() {
return "My name is " + this.name;
}
};
'name' in john; // T/F
john.name;
john['name'];
john.name = "Sunny";
john.greet();
john.age = 22;
delete john.age;
Object.keys(john).forEach((key) => console.log(key, obj[key]))
// Destructuring objects
const [name, age] = ['john', 26];
const {name, greet} = john;
const {name: var1, greet: var2} = john;
// Spread op: expand arr/obj
arr = [1,2];
foo(...arr); // foo(1,2)
[...arr, ...arr2]; //merge arrays
const newObj = {...oldObj} // pull out properties
// Rest param: merge into arr (accept unlimited extra args)
function foo(...arr) { } // args turns into an array
foo(1,2,3,4); // arr = [1,2,3,4]
function foo(bar, ...arr) { }
foo(1,2,3,4); // bar = 1, arr = [2,3,4]
// Function (obj) constructor
// returns fn that constructs obj
var Person = function(name, age) {
this.name = name;
this.age = age;
}
// `new` creates empty obj, Person fn fills it in
var jane = new Person('jane', 25);
jane instanceof Person
// Function prototype (defined once, inherited by all objs)
// `prototype` property of a function, only used after `new` keyword
Person.prototype.calcAge = function() {
console.log(2018 - this.age);
}
Person.prototype.lastName = 'Smith';
var john = new Person('john', 20);
john.calcAge();
john.lastName;
john.__proto__ === Person.prototype
john.hasOwnProperty('age');
var personProto = {
calcAge: function() {
console.log(2018 - this.age);
}
};
var john = Object.create(personProto, {
name: { value: 'john' },
age: { value: '20' }
});
// Method borrowing
john.presention.call(emily, 'foo', 'bar');
john.presention.apply(emily, ['foo', 'bar']);
john.presention.bind(emily, 'friendly'); // currying (preset arguments)
// Classses (ES6)
class SubClass extends Superclass {
constructor() {
super();
this.property = 'value';
}
methodName() { }
static method() { }
}
// Classes (ES7)
class SomeClass {
property = 'value';
methodName = () => { }
}
// Console / dialog interactions
console.log("Hello World");
console.info(john);
alert('Hey!');
var input = prompt('Enter something:');
var choice = confirm('ok?');
// Popup window
function basicPopup(url) {
popupWindow = window.open(url,'popUpWindow','height=500,width=500,left=100,top=100',
resizable='yes',scrollbars='yes',toolbar='yes',menubar='no',location='no',directories='no', status='yes');
}
// DOM selectors
document.querySelector('#id')
document.querySelector('.btn link') // "link" child of "btn" parent
document.querySelector('a[href*="${id}"'); // sel via CSS element
document.querySelectorAll('field1, field2') // sel multiple DOM elements
document.getElementById('id')
// DOM manipulation
ele.textContent = 'content';
ele.innerHTML = '<em>content</em>';
ele.setAttribute('href', ''); // set attr
ele.src = 'dice.png'; // change image
ele.dataset.goto
// add/remove CSS classes
ele.classList.add('active')
ele.classList.remove('active')
ele.classList.toggle('active')
ele.className = 'active'; // change class
// hide/show elem
ele.style.display = 'none';
ele.style.display = 'block';
// add/remove HTML
ele.insertAdjacentHTML('beforeend', html)
ele.parentNode.removeChild(ele); // del curr elem
ele.innerHTML = ''; // del all child elems
// Event listener
ele.addEventListener('click', function(event) {
event.preventDefault(); // don't refresh page
event.keyCode;
event.target; // event triggerer
event.target.closest('.btn'); // event is a child of ele
event.target.matches('.btn *'); // event is any child of ele
});
window.addEventListener('hashChange', fn)
window.location.hash
// JSON
JSON.stringify(obj); // to json
JSON.parse(jsonStr); // from json
// HTTP API
fetch('url')
.then(result => {
return result.json();
})
.then(resultJson => {
console.log(resultJson);
})
.catch(error => {} );
import axios from 'axios';
const api = axios.create({ baseURL: 'http://my.api.com' });
const response = await api.get(`/users?id=${this.id}`, { params: args });
const response = await api.post(url, post_body, { headers: { 'Content-Type': 'application/json' };
// Redirect
function redirectTo(sUrl) {
window.location = sUrl
}
// Persist data
// store inside browser (avail to client only)
localStorage.setItem('id', 'foo');
localStorage.getItem('id');
localStorage.removeItem('id');
// Cookie: sent to server on every HTTP request
document.cookie = "myContents=cookie experiment; expires=Mon, 18 Feb 2013 12:00:00 UTC; path=/";
// print cookie
document.write(document.cookie.split("=")[1])
// delete cookie
document.cookie = "myContents=cookie experiment; expires=Fri, 01 Jan 2000 12:00:00 UTC; path=/";