Skip to content

OutputNot attempted

Class method vs class-field arrow, destructured

class Btn {
label = 'ok';
handleArrow = () => this.label;
handle() {
return this?.label;
}
}
const { handle, handleArrow } = new Btn();
console.log(handle(), handleArrow());

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.

Class bodies are always strict, so a detached method gets this = undefined. A class-field arrow is created per instance in the constructor and captures that instance; this is why React class components used handleClick = () => {}.