1

I've got a lot of this in my code:

var self = this;
let foob = _.filter(data, function(d) { return d.bar == self.do(); });

Is there a way to avoid having to declare a new variable to reference a context?

el_pup_le
  • 11,711
  • 26
  • 85
  • 142
  • 2
    The arrow function notation gives you a lexical this. _.filter(data, d => d.bar === this.do()); – Shane Jun 27 '17 at 13:39
  • Yes, use an arrow function `d => ...` as the filter and replace `self` with `this`. Arrow functions lexically bind the `this` variable (= outer block's this), which is a exactly what you want. – le_m Jun 27 '17 at 13:40

0 Answers0