0

I've been reading about the this keyword on MDN. In their article they have mentioned the following example:

<button onclick="alert((function(){return this}()));">
  Show inner this
</button>
  • What is the purpose of brackets () after return statement {return this}()?
user31782
  • 7,087
  • 14
  • 68
  • 143
  • 2
    To actually call the anonymous function – Damiano Stoffie Apr 19 '16 at 06:50
  • Those brackets make the function a **self-invoking** anonymous function. – agdhruv Apr 19 '16 at 06:52
  • @ag_dhruv But it works even if we don't enclose the whole function and function call inside closed brackets, e.g. `( function(){return this}() )` works as well. The other post says enclosing function definition inside brackets converts the function definition into an expression. – user31782 Apr 19 '16 at 06:55

0 Answers0