For example, here I'm not using braces around my lambda expression, so whenever item.condition is not 1 it breaks the loop.
_.each(items, (item) => item.visible = (item.condition === 1));
This compiles down to:
_.each(items, function(item) { return item.visible = (item.condition === 1); });
See how it inserted a return statement? That's not we wanted!
By simply adding curly braces the return is removed and you get the correct behavior.
_.each(items, (item) => { item.visible = (item.condition === 1); });
It compiles to:
_.each(items, function(item) {
item.visible = (item.condition === 1);
});
So maybe I should start using braces when I don't want to explicitly return a value. But it looks so much cleaner the other way when you just want to do a simple one-liner. So we'll see...
No comments:
Post a Comment