A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript 🚀 Don't miss our Quizzes!
Let's chat: @nairihar
Post #3003
3.26K
- ❤ 6
JA @javascript
Showing posts older than #3004 · Back to latest
const wm = new WeakMap();
const obj1 = { name: 'Sarah' };
const obj2 = { name: 'Mike' };
wm.set(obj1, 'developer');
wm.set(obj2, 'designer');
console.log(wm.get(obj1));
console.log(wm.has(obj2));
console.log(wm.get({ name: 'Sarah' }));
console.log(wm.delete(obj1));
console.log(wm.has(obj1));

class Calculator {
constructor(value) {
this.value = value;
}
add(num) {
this.value += num;
return this;
}
getValue = () => this.value;
}
const calc = new Calculator(10);
const addMethod = calc.add;
const getValueMethod = calc.getValue;
addMethod.call({value: 5}, 3);
console.log(calc.getValue());
console.log(getValueMethod());const operations = {
value: 10,
multiply: function(x) {
return this.value * x;
},
arrow: (x) => {
return this.value * x;
},
nested: function() {
const inner = () => this.value * 2;
return inner();
}
};
console.log(operations.multiply(3));
console.log(operations.arrow(3));
console.log(operations.nested());

console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
Promise.resolve().then(() => {
console.log('5');
setTimeout(() => console.log('6'), 0);
});
console.log('7');
function memoize(fn) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn.apply(this, args);
cache.set(key, result);
return result;
};
}
const fibonacci = memoize(function(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
});
console.log(fibonacci(10));
console.log(fibonacci.cache?.size || 'undefined');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { ...obj1 };
const obj3 = obj1;
obj2.a = 10;
obj2.b.c = 20;
obj3.b.c = 30;
console.log(obj1.a);
console.log(obj1.b.c);
console.log(obj2.a);
console.log(obj2.b.c);
console.log(obj3.a);
console.log(obj3.b.c);