-1

When using meteor with ecmascript's arrow notation, using 'this' to access the value described in the documentation always fails. For example:

 Template.temp.onCreated(()=> {
      this.var = new ReactiveVar("")
 })

and then access it via:

 Template.instance().var

always fail. This is consistant in almost any use of 'this' in meteor, client or server side. When inspecting from the client, I can see that "this" is compiled into "_this" which always cause the code to fail.

the code is compiled into:

 _this = this
 Template.temp.onCreated(function() {
      _this.var = new ReactiveVar("")
 })

Any advice is appreciated.

Jon Surrell
  • 8,394
  • 6
  • 44
  • 52
AhHatem
  • 1,358
  • 1
  • 13
  • 22

1 Answers1

1

This is because arrow functions are bound to the scope at the time they are created. In your case, at the time of creation the scope is global and your function's scope is, therefore, the global scope.

In conclusion, this behavior is the correct one by design.

You should not use the arrow functions when it is not appropriate. It is not just a "short-hand" to save typing a few characters.

MasterAM
  • 15,074
  • 6
  • 40
  • 62