Fat arrow functions returns 'this' as 'undefined'


#1

Template events:

'click li':()=>{ console.log(this._id) }

returns: undefined

but if I do it the old fashion way:

'click li':function() { console.log(this._id) }

returns the _id of the object.

Any input about the matter will be greatly appreciated.


#2

This is correct and a indicator why you should not overuse fat arrows.

Fat arrow functions binds the context automatically. So, in this case mostly this will be window object.

So, blaze can’t set the data context to that. So, you should not use fat-arrow functions here.


#3

You can use this short version of function declaration:

'click li'() { console.log(this._id);  }

#4

‘click li’() { console.log(this._id) } You can write like this