function Person() {
this.age = 0;
this.intervalId = setInterval(function growUp() {
console.log('[Person] growUp called. this.age:', this ? this.age : undefined);
}, 1000);
}
function PersonFixed() {
this.age = 0;
this.intervalId = setInterval(() => {
this.age++;
console.log('[PersonFixed] Arrow function called. this.age:', this.age);
}, 1000);
}
console.log('1. Testing Person (Broken `this` binding)...');
const p = new Person();
setTimeout(() => {
console.log(' Person age after 1.1s:', p.age); clearInterval(p.intervalId);
console.log('\n2. Testing PersonFixed (Arrow function)...');
const pf = new PersonFixed();
setTimeout(() => {
console.log(' PersonFixed age after 1.1s:', pf.age); clearInterval(pf.intervalId);
}, 1100);
}, 1100);