Pass a Class
function callMethods(evt) {
const e = new evt
e.one()
e.two()
}
callMethods(class {
one() {
console.log('one')
}
two() {
console.log('two')
}
})
This is so tempting for something I want to do… but too strange to use probably… maybe…
function callMethods(evt) {
const e = new evt
e.one()
e.two()
}
callMethods(class {
one() {
console.log('one')
}
two() {
console.log('two')
}
})
This is so tempting for something I want to do… but too strange to use probably… maybe…
const map = new Map()
map.set(1, 'one')
console.log(map.get(1))
const weakMap = new WeakMap()
// this will fail with an error:
// weakMap.set(1, 'one')
// console.log(weakMap.get(1))
Noticed this gotcha the other day. For some reason WeakMap
can’t have integers as keys. After years of using WeakMap
I guess I’ve only ever used objects as keys and assumed it could just have keys of any type. Using a Map
instead solves the problem, but you’ll need to be careful to manager your references to properly clear the Map
. Anyway, just one of those things… Maybe I’ll write more about it when I have some time to dig deeper.
const obj = { 11: 'eleven', 23: 'twenty-three', 1: 'one', 2: 'two', '-1': 'negative 1' };
console.log(Reflect.ownKeys(obj))
I noticed that for in..
over an object was giving me weirdly consistent results across all browsers the other day and stumbled upon this.
Great news, but it’s Reflect.ownKeys
for me… now that IE11 is dying/dead.
const collection = [
{ name: 'Joe' },
{ age: 31 },
{ job: 'programmer' }
];
const merged = Object.assign({}, ...collection);
console.log(merged);
Combine (“assign”) entire array of objects.
Saw this here… figured it was worth a quick post.
const book = {
get name() {
return this.title ?? 'no title';
},
set name(val) {
document.
body.
innerHTML += `name changed to "${val}"<br>`;
this.title = val;
}
};
console.log(book.name);
book.name = 'Math Book';
book.name = 'JavaScript Book';
Getter setter syntax for Object literal.