The topic of arrow functions is commonly discussed, but I've been unable to find an answer to the following scenario.
Let's consider this example from an Angular 4 Directive:
export class MouseParallaxDirective implements AfterViewInit {
constructor(private element: ElementRef) {
}
ngAfterViewInit() {
// In this case, `this` refers to the angular component object
let movementStrength = 25;
let height = movementStrength / window.innerHeight;
let width = movementStrength / window.innerWidth;
let parallaxElement = this.element.nativeElement;
window.onmousemove = (e) => {
// Here, `this` should refer to the same object as per function scope preservation by arrow function
let pageX = e.pageX - (window.innerWidth / 2);
let pageY = e.pageY - (window.innerHeight / 2);
let newvalueX = width * pageX * -1 - 25;
let newvalueY = height * pageY * -1 - 50;
parallaxElement.style.backgroundPosition = newvalueX + "px " + newvalueY + "px";
};
}
}
Shouldn't both instances of this
point to the identical object when using an arrow function to maintain the outer scope?