Middle
```javascript const obj = { name: 'Alex', say() { console.info(this.name); } }; const fn = obj.say; fn(); obj.say(); ```
sobes.tech AI
Answer from AI
This code does the following:
const obj = {
name: 'Alex',
say() { console.info(this.name); }
};
const fn = obj.say;
fn(); // undefined
obj.say(); // 'Alex'
When obj.say() is called, the this context inside the method points to the obj object, so it outputs 'Alex'.
However, when assigning const fn = obj.say;, the say function loses its binding to the object, and calling fn() makes this either undefined (in strict mode) or the global object (in non-strict mode). As a result, this.name is undefined, and it outputs undefined.
To preserve the context, you can use bind:
const fn = obj.say.bind(obj);
fn(); // 'Alex'