0

I have the following code where I am using the arrow functions to get my work done. This works perfectly in Chrome and Firefox as they support arrow functions.

this._subscription = this._modal.shown.subscribe(
  () => this._el.nativeElement.focus());

But, this doesn't work properly in IE 11. So, I am trying to convert this arrow function into an anonymous self invoking function. I did the following:

this._subscription = this._modal.shown.subscribe(
  (function(){ this._el.nativeElement.focus()})());

But the above code isn't working. Am I doing something wrong? How can I do this. Any suggestions and help would be appreciated.

6
  • 4
    If you want to pass it, why are you invoking it immediately? The arrow function isn't immediately invoked. Commented Oct 27, 2016 at 22:39
  • So, how can I do the equivalent of the first code bit in the next code bit? @squint Commented Oct 27, 2016 at 22:42
  • 2
    @Marley Don't immediately execute the function, remove the () at the end in (function(){ ... })() so the anonymous function is not invoked. In your first code snippet you are giving the function reference and not calling it. Commented Oct 27, 2016 at 22:45
  • when you Use Arrow function there is no need to bind the function.try to use Arrow function in the name less functions Commented Jul 4, 2019 at 12:34
  • Extraneous () – there is no need to make an IIFE at all, just write a regular function expression. That is all. Commented Dec 26, 2024 at 12:55

1 Answer 1

0

Do this:

this._subscription = this._modal.shown.subscribe(
    (function(){ this._el.nativeElement.focus();}
).bind(this));

Here, as @harmic mentioned in the above answer's comment, this would no longer be referencing the previous value, as the scope changes in this case. So, you got to bind this to make it work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.