Skip to content

Instantly share code, notes, and snippets.

@iOliverNguyen
Last active September 16, 2022 01:14
Show Gist options
  • Save iOliverNguyen/6420004eb084688e9672b15cf5f574dc to your computer and use it in GitHub Desktop.
Save iOliverNguyen/6420004eb084688e9672b15cf5f574dc to your computer and use it in GitHub Desktop.
class Counter {
constructor() {
this.name = 'Counter';
this.count = 0;
}
inc() {
this.count++;
}
}
class Counter {
name = 'Counter';
#count = 0; // private field!
inc() {
this.#count++;
}
}
class Counter {
name = 'Counter';
#count = 0;
static isCounter(obj) {
return #count in obj;
}
}
const counter = new Counter();
Counter.isCounter(counter); // true
const A = [2, 4, 6, 8, 10]
A.at(-1) // 10
const S = "Hello World"
S.at(-1) // 'd'
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
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
let hasOwnProperty = Object.prototype.hasOwnProperty;
if (hasOwnProperty.call(object, 'foo')) {
console.log('has property foo');
}
object.hasOwnProperty('foo')
if (Object.hasOwn(object, 'foo')) {
console.log('has property foo');
}
await fetch('https://example.com/data.csv')
.catch((err) => {
throw new Error('failed to get: ' + err.message);
})
await fetch('https://example.com/data.csv')
.catch((err) => {
throw new Error('failed to get', { cause: err })
})
.catch((err) => {
console.log('cause', err.cause)
})
#!/usr/bin/env node
'use strict';
console.log(1);
#!/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