I’m hoping I got the right area to ask for this…
Below is some code from two files I’m working on. When I change anything with ‘this.’ in the function to an arrow function, the code no longer functions as expected.
Is someone able to explain what I’m doing wrong? Do I need to do something differently when using arrow functions? Am I using ‘this.’ incorrectly to begin with? Should I not be using arrow functions in this way?
Template.jobList.helpers({
jobs: ()=> Jobs.find({}, {sort: {jobDate: 1}}),
formattedDate: function() {
let d = this.jobDate;
let e = formatDate(d);
return e;
},
shortDesc: function () {
if (this.description.length > 40) {
return this.description.substr(0, 50) + '...';
} else {
return this.description
}
},
jobID: function () {
let a = this.jobNum;
let e = this.jobType.substr(0, 1);
return e + a
}
});
I’ve gone through that documentation, as well as the ‘this’ article on MDN, however I’m not sure how if it would work with data that is being pulled from an #each loop
Arrow functions have a lexical ‘this’, meaning they share the ‘this’ from the context/scope they are written in.
So this means that:
Template.jobList.helpers({
formattedDate: () => {
let d = this.jobDate; // 'this' is currently set to window, not the job document
let e = formatDate(d);
return e;
}
});
I hope that makes sense. Your code should be written like this:
The response from @huttonr explains arrow functions vs regular functions well. He just overlooked that you posted actually working code, or at least it seems like that. If he was correcting your code just for (much) improved style of using ES6, then that’s actually well done.
So yes, your code is fine. Beware of the this rules for arrow vs regular functions (ask more if it’s still not clear), and then usually use the notation @huttonr gave you above for things like methods defined as part of objects or classes, and use arrow functions for all kinds of situations where you need to pass a callback or other function and you need your this to be unchanged from the outer scope (or don’t care), but if the this of your callback will be set to something meaningful by the code calling back your callback, then you definitely want to use a regular function (like in that Blaze case above where Blaze will set your this to something you want to work with).