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 #1650
5.63K
- π€£ 23
- β€ 5
- π₯ 4
- π€© 2
JA @javascript
Showing posts older than #1651 Β· Back to latest
const obj = {
value: 1,
increment() {
this.value++;
}
};
const increment = obj.increment;
increment();
console.log(obj.value);
const obj = {
data: [1, 2, 3],
getData() {
return this.data.map(x => x * 2);
}
};
const newData = obj.getData.bind({ data: [4, 5, 6] })();
console.log(newData);

const obj = {
count: 0,
increment() {
this.count++;
},
getCount() {
return (() => this.count)();
}
};
obj.increment();
obj.increment();
console.log(obj.getCount());

const obj = {
value: 42,
getValue() {
return Math.max(this.value, 50);
}
};
const obj2 = {
value: 25,
getValue: obj.getValue
};
console.log(obj.getValue());
console.log(obj2.getValue());

const obj = {
a: 1,
b: 2,
getSum() {
return this.a + this.b;
}
};
const cloneObj = { ...obj, getSum() { return this.b + this.a; } };
console.log(obj.getSum());
console.log(cloneObj.getSum());