I have a question if anybody can answer.
Here's the code:
https://stackblitz.com/edit/angular-chxuhg
There's a service in demo.service.ts, it has two methods, both of them are logged, one has Function.prototype.bind used on it, one is arrow function, in console I can run the 'someOtherMethod' without any problems, but 'someMethod' throws error, but if I run it using fn.call(instance) it works
why is that happening?
Here's the main code example
import { Injectable } from '@angular/core';
@Injectable()
export class ExampleService {
private demoParameter = 'just some value';
constructor() {
console.log('Instance', this);
const newFn = this.someMethod.bind(this);
console.log('Manual binded Method', newFn);
console.log('Auto binded method', this.someOtherMethod);
}
someMethod() {
console.log(this.demoParameter);
}
someOtherMethod = () => {
console.log(this.demoParameter);
}
}
This was flagged as duplicate even though it's not, it's a chrome bug when you assing logged function to global variable temp1
, in Firefox this works as expected