Fat arrow functions returns 'this' as 'undefined'

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.

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 Likes

You can use this short version of function declaration:

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

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

1 Like