Last active
September 16, 2022 01:14
-
-
Save iOliverNguyen/6420004eb084688e9672b15cf5f574dc to your computer and use it in GitHub Desktop.
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
class Counter { | |
constructor() { | |
this.name = 'Counter'; | |
this.count = 0; | |
} | |
inc() { | |
this.count++; | |
} | |
} |
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
class Counter { | |
name = 'Counter'; | |
#count = 0; // private field! | |
inc() { | |
this.#count++; | |
} | |
} |
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
class Counter { | |
name = 'Counter'; | |
#count = 0; | |
static isCounter(obj) { | |
return #count in obj; | |
} | |
} | |
const counter = new Counter(); | |
Counter.isCounter(counter); // 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 A = [2, 4, 6, 8, 10] | |
A.at(-1) // 10 | |
const S = "Hello World" | |
S.at(-1) // 'd' |
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 A = [1, 20, 3, 40, 5]; | |
function findBackward(A, predicate) { | |
for (let i = A.length-1; i>=0; i--) { | |
if (predicate(A[i])) { | |
return A[i]; | |
} | |
} | |
return -1; | |
} | |
findBackward(A, x => x % 10 === 0); // 40 | |
// be careful with this function! | |
A.reverse().find(x => x % 10 === 0); // 40 |
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 A = [1, 20, 3, 40, 5]; | |
A.find(v => v%10 === 0) // 20 | |
A.findLast(v => v%10 === 0) // 40 | |
A.findIndex(v => v%10 === 0) // 1 | |
A.findLastIndex(v => v%10 === 0) // 3 | |
A.findLastIndex(v => v === 0) // -1 |
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 hasOwnProperty = Object.prototype.hasOwnProperty; | |
if (hasOwnProperty.call(object, 'foo')) { | |
console.log('has property foo'); | |
} |
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
object.hasOwnProperty('foo') |
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
if (Object.hasOwn(object, 'foo')) { | |
console.log('has property foo'); | |
} |
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
await fetch('https://example.com/data.csv') | |
.catch((err) => { | |
throw new Error('failed to get: ' + err.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
await fetch('https://example.com/data.csv') | |
.catch((err) => { | |
throw new Error('failed to get', { cause: err }) | |
}) | |
.catch((err) => { | |
console.log('cause', err.cause) | |
}) |
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
#!/usr/bin/env node | |
'use strict'; | |
console.log(1); |
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
#!/usr/bin/env node | |
export {}; | |
console.log(1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment