r/node • • Apr 14 '24

JavaScript Functions - 4 Ways

Post image
208 Upvotes

79 comments sorted by

View all comments

38

u/damnNamesAreTaken Apr 14 '24

As someone who doesn't really use JavaScript much, what was wrong with the function declaration form and why are the rest needed? I learned JS before the newer ways existed and never really kept up.

82

u/travybongos69 Apr 14 '24

Arrow functions don't have their own this which can be useful at times

10

u/damola93 Apr 15 '24

Came here to say this.

1

u/121131121 Apr 15 '24

This might be the most underrated explanation here.

2

u/Think_Discipline_90 Apr 15 '24

"At times" try 99% of cases.

The only times I use oldschool function declaration is if I want typed overloads

3

u/[deleted] Apr 15 '24 edited Apr 14 '26

Data Brokers don't stand a chance because I mass delete all of my content using Redact - No AI training on my data, thank you very much.

cooing punch encouraging fuzzy longing squash bright fact thought lock

30

u/joelangeway Apr 14 '24

When function is a statement, it has the same binding rules as var. When it is an expression, a name is optional but if it is provided it is scoped to the function body. The fat arrow operator is just like a function expression accept it does .bind(this) at the end and can have an expression instead of a code block for a body.

JavaScript has a lot of warts, but the way it lets one sling functions around …. I just love it.

8

u/SparserLogic Apr 14 '24

They are handled differently by the compiler when it comes to where the function is scoped in memory. Variables are treated like other variables rather than globally like a traditional function.

5

u/RaveMittens Apr 14 '24

the compiler

The interpreter?

5

u/MaxUumen Apr 14 '24

The engine

3

u/RaveMittens Apr 14 '24

The runtime

26

u/SparserLogic Apr 14 '24

The thing that does the thing.

3

u/noXi0uz Apr 15 '24

All major JS engines use a JIT compiler

4

u/armano2 Apr 14 '24

its actually a compiler, compilation is usually done "before" execution, but technically this is still a compilation

read more. https://v8.dev/blog/background-compilation

1

u/shitty_mcfucklestick Apr 15 '24

A series of short gasps were heard, and the room fell silent as all eyes turned to /u/SparserLogic. Did they really know JavaScript, or are they an undercover desktop C programmer person trying to infiltrate the sub?

Tune in next Friday!

2

u/SparserLogic Apr 15 '24

Jokes on you all, I'm just three raccoons in a trench coat.

1

u/eruwinuvatar Apr 14 '24

The function expressions can be inlined (you define them where they are used) and if you are going to inline them anyway, it's better to be as concise or as short as possible:

const sumOfSquaresOfEvens = nums
  .filter(x => x % 2 === 0)
  .map(x => x * x)
  .reduce((x, y) => x + y)

You could declare those 3 functions beforehand but what's the point in declaring one-liner functions?

2

u/ravepeacefully Apr 15 '24

Testing, readability, reducing complexity overall

1

u/nodeymcdev Apr 16 '24

I’m sure you meant the opposite of what I’m about to say but declaring three functions is generally much more complex and less readable than just chaining array methods

1

u/ravepeacefully Apr 16 '24

No. Functions should be as small as possible.

I get what you mean, it’s totally appropriate for VERY simple operations to be chained, but overall functions should be as short and simple as possible.

You should absolutely not have 120 line functions that carry out a multitude of operations.

1

u/nodeymcdev Apr 16 '24

Of course it depends on the situation, but if the function is only being used in that one place it makes sense that it’s not defined but just written inside of a chained array method. Otherwise you’re forced to jump around a file searching for functions by name. Much more readable to just start at the variable name “sumOfSquaresOfEvens” and read what it does where it’s doing it.

1

u/ravepeacefully Apr 16 '24

So what you’re saying is you don’t write tests?

1

u/nodeymcdev Apr 16 '24

Of course I do. I split up my code into functions where it makes sense to do so. sumOfSquaresOfEvens is a simple function. In this case you aren’t going to test each individual method inside of the chain. You are going to test the entire thing together.

→ More replies (2)
→ More replies (15)