1

I'm learning some more ES6:

const _findCartItem = ( item ) => {
    return _cartItems.find( cartItem => cartItem.id === item.id);
};

converts to:

var _findCartItem = function _findCartItem(item){
    return _cartItems.find(function(cartItem){
        return cartItem.id == item.id;
    })
};

However, is there a difference between this and the following? Is this dual declaration necessary?

var _findCartItem = function(item){
    return _cartItems.find(function(cartItem){
        return cartItem.id == item.id;
    })
};
userqwert
  • 2,193
  • 5
  • 28
  • 50
  • But - `var aa = function bb() {}; aa.name` outputs `"bb"` – eithed Nov 16 '16 at 18:02
  • 3
    @userqwert if you do `var func = function fuc(){}` the function is _named_. It just means that it will show up in debug tools and stack traces with the name `func` as opposed to `(anonymous block)`. The fat arrow function names the function after the variable it's assigned to, so it's similar to doing `var func = () => {};` – VLAZ Nov 16 '16 at 18:03
  • @vlaz that's very helpful, post it as an answer! Or not, some dude closed it. So a *named* funtion literally just has a *name* property? – userqwert Nov 16 '16 at 18:09
  • @userqwert yes, that's it. It doesn't change anything than debug output. Should be noted that you can name your function anything, e.g., `var a = function b() {}` is _valid_ - however, you cannot now do `b()` as that's not what the function is assigned to. – VLAZ Nov 16 '16 at 18:16
  • @vlaz brill, thanks a lot – userqwert Nov 16 '16 at 18:30

1 Answers1

2

Conceitualy, no, both will do the same. Syntactically, yes. First one is passing a reference of a named function to a variable, second a anonymous function.

Using first option is prefereble because you get better call stacks.

Cleiton
  • 17,663
  • 13
  • 46
  • 59