0

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

nikagar4
  • 680
  • 4
  • 8
  • 22
  • must be an issue with transpilation or devTools acting up (especially since the server is running in development mode). Try compiling the code and running the output to see if issue persists – Christian Santos Apr 12 '19 at 19:18
  • You can see the transpiled code and it's basically the same, one logs the variable, the other logs undefined, (without Injectable and all that angular stuff) – nikagar4 Apr 15 '19 at 07:47

0 Answers0