This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ajax+XHR -> fetch API | |
| document.getElementById('button1').addEventListener('click', getText); | |
| document.getElementById('button2').addEventListener('click', getJson); | |
| document.getElementById('button3').addEventListener('click', getExternal); | |
| // Get local txt data | |
| function getText() { | |
| fetch('test.txt') | |
| .then(res => res.text()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * EasyHTTP Library | |
| * Library for making HTTP requests | |
| * | |
| * @version 2.0.0 | |
| * @author Eric Whale | |
| * @license MIT..? | |
| */ | |
| class EasyHTTP { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function myFunc() { | |
| const promise = new Promise((resolve, reject) => { | |
| setTimeout(() => resolve('Hello'), 1000); | |
| }); | |
| const err = false; | |
| if (!err) { | |
| const res = await promise; // wait until promise is resolved | |
| return res; | |
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = { email: '[email protected]' }; | |
| try { | |
| if (!user.name) { | |
| // throw 'User has no name'; | |
| throw new SyntaxError('User has no name'); | |
| } | |
| } catch (e) { | |
| console.log(`User Error: ${e.message}`); | |
| // console.log(e.message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let reg, result, str; | |
| reg = /hello/g; // global search | |
| reg = /hello/; | |
| reg = /hello/i; // i == flag : case insensitive | |
| console.log(reg); | |
| console.log(reg.source); | |
| // exec() - result in array or null | |
| result = reg.exec('hello world'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Iterator | |
| function nameIterator(names) { | |
| let nextIndex = 0; | |
| return { | |
| next: function () { | |
| return nextIndex < names.length | |
| ? { value: names[nextIndex++], done: false } | |
| : { done: true }; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const data = [ | |
| { | |
| name: 'John Doe', | |
| age: 32, | |
| gender: 'male', | |
| lookingfor: 'female', | |
| location: 'Boston MA', | |
| image: 'https://randomuser.me/api/portraits/men/82.jpg' | |
| }, | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Destructuring Assignment | |
| let a, b; | |
| [a, b] = [100, 200]; | |
| // Basic + (rest) | |
| [a, b, c, ...rest] = [100, 200, 300, 400, 500]; | |
| // # OBJECT # + (rest) | |
| ({ a, b, ...rest } = { a: 100, b: 200, c: 300, d: 400, e: 500 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MAP (key-value pair) | |
| const map = new Map(); | |
| const key1 = 'string key', | |
| key2 = {}, | |
| key3 = function () {}; | |
| map.set(key1, 'value of key1'); | |
| map.set(key2, 'value of key2'); | |
| map.set(key3, 'value of key3'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SET - unique values | |
| const set1 = new Set(); | |
| set1.add(100); | |
| set1.add('A string'); | |
| set1.add({ name: 'John' }); | |
| set1.add(true); | |
| set1.add(100); |