-1

Can someone explain why we create a function and then use 'let' to assign the anonymous function to a variable? What's the point of writing it like that?

function buildName(firstName: string, ...restOfName: string[]) {
    return firstName + " " + restOfName.join(" ");
}

let buildNameFun: (fname: string, ...rest: string[]) => string = buildName;
  • Because it's less typing? – Jared Smith Oct 23 '18 at 15:00
  • Without context it's hard to say why. Maybe they wanted to have two names for the same function? – jcalz Oct 23 '18 at 15:01
  • Context of `this` object. go through the ref to get the better understanding https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Suryan Oct 23 '18 at 15:06
  • @Suryan but the function does not use `this` in any way... – Nurbol Alpysbayev Oct 23 '18 at 15:16
  • @NurbolAlpysbayev what i mean was arrow function does not have its own this instead always points towards the object containing the function – Suryan Oct 23 '18 at 15:19
  • @Suryan Yes I understood your point, but it does not change anything. At least for this particular case, where `this` is never accessed. – Nurbol Alpysbayev Oct 23 '18 at 15:22
  • @NurbolAlpysbayev accepted then for this case i'll go with Jared Smith for shorter syntax – Suryan Oct 23 '18 at 15:25
  • Probably because someone thinks arrow functions are the only correct way to write functions. – Kevin B Oct 23 '18 at 15:41
  • There are many ways to define functions. The original decided to use two different ways. There doesn't have to be a reason behind it. There is no *technical* reason to choose one over the other in this case, if that's what you are asking. It's good practice to be *consistent*, everybody can do as they please. – Felix Kling Oct 23 '18 at 20:17

2 Answers2

1

I have a few guesses:

  • Someone doesn't care about resources (CPU, disk) at all
  • Someone wanted to look fancy (by adding the arrow function without any legit reason)

Seriously, there is no good reason to do this.

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • There is zero difference in resources for this. – loganfsmyth Oct 23 '18 at 15:54
  • @loganfsmyth I agree that this is almost zero impact for performance, but of course, not exactly zero. Because, if, following your logic one creates millions of useless variables there wouldn't be any impact neither on program size, nor on speed of parsing such code. But I see that you have about 100 000 scores, and I can infer that what you really meant was "There is *almost* zero difference in resources for this". – Nurbol Alpysbayev Oct 23 '18 at 16:05
  • A function declaration and a function assigned to a variable both create a variable with a single function in it. The memory and resources are the same. – loganfsmyth Oct 23 '18 at 16:26
  • @loganfsmyth I believe if you had an ability to gain this knowledge, you can as well read once again and see that I didn't mention memory, but *size* and *speed of parsing such code*. And size relates to disk resource, speed of parsing relates to CPU (oh and memory, too, but in another way). – Nurbol Alpysbayev Oct 23 '18 at 16:29
  • The difference in these two forms is a couple bytes one way or the other, it's not going to have any appreciable difference on parse and transfer times. – loganfsmyth Oct 23 '18 at 16:40
  • @loganfsmyth So "There is *almost* zero difference in resources for this"? Please understand I never insisted that this is significant performance dropping, but still some. – Nurbol Alpysbayev Oct 23 '18 at 16:41
  • To be clear, the assertion in your answer is "there is no good reason to do this.". My point is that your argument doesn't actually support that. Adding a few extra bytes does not make the arrow approach unreasonable. – loganfsmyth Oct 23 '18 at 16:43
  • @loganfsmyth Something is unreasonable if there is no reason to do it. Or did you find it? Why not post it then. – Nurbol Alpysbayev Oct 23 '18 at 16:48
  • 1
    Something is unreasonable when doing it would be detrimental. Sometimes there are just many ways to do something and none are better than others. This question is entirely opinion-based. There are plenty of reasons to have a preference, but very few reasons that it strictly matters. – loganfsmyth Oct 23 '18 at 16:51
  • @loganfsmyth I think you just missed the point of the question, it was not about preference, not about choosing something. The guy who wrote the code assigned old-fashioned function to a variable. This has no good reason, because he could just as well use the function itself, without creating a variable. I wanted to stop this discussion, and every time you make me continue. Thanks though for communicating, and sorry if I was mean somewhere above. – Nurbol Alpysbayev Oct 23 '18 at 16:58
1

I would say that it's because you have to assign an arrow function to a variable in order to use it elsewhere. There are couple of differences between arrow functions and es5 functions so if someone needed arrow function functionalities, that's the only way he or she could declare it.

Also, keep in mind that let and const are not hoisted, contrary to functions declared through the function keyword.

foo() // this will work
function foo () {
  console.log('foo')
}
bar() // this will not
let bar = () => console.log('bar');
rufus1530
  • 765
  • 4
  • 19