What does this print?
const obj = {
x: 41,
getX() { return this.x; }
};
const fn = obj.getX;
console.log(fn(), obj.getX.call({ x: 99 }));
- 41 99
- undefined 99
- 99 99
- TypeError is thrown
0
voters
Quelly
What does this print?
const obj = {
x: 41,
getX() { return this.x; }
};
const fn = obj.getX;
console.log(fn(), obj.getX.call({ x: 99 }));
Quelly
Choosing “undefined 99” because Extracting getX loses its receiver so plain fn() has undefined this in strict mode, while call binds x to 99.
MechaPrime
I would pick “undefined 99” because Extracting getX into fn drops the receiver so fn() sees undefined this in strict mode, while call explicitly binds { x: 99 }.
WaffleFries
I would pick “undefined 99” because Extracting getX into fn loses the receiver so fn() has undefined this in strict mode, while call sets this to { x: 99 }.
Arthur
:: Copyright KIRUPA 2024 //--