Skip to content

OutputNot attempted

Regular callback inside map, with and without thisArg

const obj = {
prefix: '>',
list: ['a', 'b'],
print() {
return this.list.map(function (x) {
return this?.prefix + x;
});
},
printFixed() {
return this.list.map(function (x) {
return this.prefix + x;
}, this);
},
};
console.log(obj.print(), obj.printFixed());

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

A regular callback passed to map is called with no receiver → this is undefined → undefined + "a" coerces to "undefineda". map's 2nd argument (thisArg) sets it. An arrow callback would also fix it.