Skip to content

OutputNot attempted

Method, detached, arrow, nested

const obj = {
name: 'outer',
regular() {
return this?.name;
},
arrow: () => this?.name,
nested() {
const inner = () => this.name;
function legacy() {
return this?.name;
}
return [inner(), legacy()];
},
};
const { regular } = obj;
console.log(obj.regular(), regular(), obj.arrow(), obj.nested());

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.

Regular fn = WHO CALLED ME; arrow = WHERE WAS I WRITTEN. obj.regular() → obj. regular() → no receiver → undefined. arrow is written in an object literal (not a scope) so it borrows module-level this → undefined. inner borrows nested's this (obj). legacy() is a plain call → undefined; it does NOT look outward.