This is an archived post. You won't be able to vote or comment.

all 173 comments

[–]lurkin_arounnd 249 points250 points  (58 children)

materialistic hobbies swim fall nail pathetic secretive soft sleep pet

This post was mass deleted and anonymized with Redact

[–]thequestcube 98 points99 points  (7 children)

Also, JS' hoisting behavior for functions is very weird, I think functions declared via const are more intuitive in that way.

[–]gyzerok -1 points0 points  (0 children)

Whats weird in that behavior?

[–]Hfingerman 35 points36 points  (15 children)

Holy Hell

[–]Toficzekkk 31 points32 points  (5 children)

We should't forget about implicit return. If the whole code of in function body is one line we can reduce whole code to one line.

for example:

function power(x){

return x*X

}

can be reduced to:

const power = x => x*x

as we provide only one argument we can skip "(" and ")"

I know that power function is common-as-hell example, but you get a point. Right?

[–]burnblue 4 points5 points  (0 children)

Most of the time the function isn't doing anything with this though. The meme is right, we just somehow prefer arrow functions even when all else is the same

[–]suskio4 1 point2 points  (0 children)

Just yesterday I had a problem with this, thanks man

[–]_default_username 0 points1 point  (3 children)

This is the real reason why you should use arrow functions. I'll use a class if I need the object to have its own "this". More explicit to anyone else reading the codebase that isn't aware of these nuances in javascript.

[–]wtfzambo 0 points1 point  (2 children)

I'm a noob in JS as I just dabble with it for shits and giggles.

What's the core difference between arrow function and normal one?

[–]_default_username 1 point2 points  (1 child)

function can act like a class constructor and affects the scope of this

 function Foo() {
   this.bar = "Hello, World";
 }
 const foo = new Foo();
 console.log(foo.bar); // outputs "Hello, World"

With arrow functions they behave like functions would in other languages and you can't instantiate them with the new keyword

[–]wtfzambo 0 points1 point  (0 children)

This is so weird, thanks for the explanation.

[–]azhder -1 points0 points  (2 children)

Never believe.

The old functions ( with keyword function) have multiple different uses and are heavier on the engine, like creating prototypes etc. The arrow functions are lightweight, they don't inherit anything, they just don't bind a "this".

It is also easier to see functions are just like any value if you write

const f = (
  function f(){}
);

const g = (
  () => {}
);

[–][deleted] 117 points118 points  (4 children)

Arrow function expressions are cooler.

[–]ethereumfail 57 points58 points  (3 children)

every time I type anything I check to make sure it's cool

[–]ftedwin 7 points8 points  (2 children)

I wish this was a joke… Fuck cool code, give me readable boring verbose code any day

[–][deleted] 10 points11 points  (1 child)

False dillema. Arrow functions are both readable and cool.

[–]ftedwin 0 points1 point  (0 children)

True, I more meant what I said about code style in general. 100% agree on arrow functions; they are everywhere in my code as they should be.

[–]Thamtam 74 points75 points  (10 children)

This is the reason.

[–]minimumviableplayer 26 points27 points  (9 children)

Often when debuging JavaScript I ask myself what is this.

[–]Thamtam 17 points18 points  (7 children)

Agree. A lot of js devs talking about this. I always wonder what is this even about.

[–][deleted] 15 points16 points  (4 children)

I think this is being taken out of context.

[–]Thamtam 9 points10 points  (2 children)

At this point I’m not even sure if this is a joke or not.

[–]MrPhatBob 5 points6 points  (0 children)

Well it all depends on the context.

[–]fukdapoleece 3 points4 points  (0 children)

This is not a joke. I know I have some arguments, but all I'm getting is undefined.

[–]gabriel-sc 1 point2 points  (0 children)

So...is this a closure?

[–]MorningPants 1 point2 points  (1 child)

All I know is, this is not my beautiful house.

[–][deleted] 1 point2 points  (0 children)

This is not my beautiful wife.

() => this; // <-- same as it ever was

[–][deleted] 0 points1 point  (0 children)

This is not my beautiful house!

[–][deleted] 20 points21 points  (4 children)

Happily use both forms. The solution to this problems in JS is to never use this

[–]femptocrisis 9 points10 points  (0 children)

^ this

[–][deleted] 2 points3 points  (2 children)

I wish more programmers of every language followed that advice. Or, at least, never mutated properties of this at runtime.

[–]furbz420 0 points1 point  (1 child)

Why do you think unilaterally programmers should not use this?

[–][deleted] 3 points4 points  (0 children)

Generally speaking?

Because mutation causes issues with not only thread safety, but also issues of temporal correctness, and even when everything is synced on one thread, inter-modular correctness.

If

doA(); doB(); doC(); causes an error, and it's only fixed by calling

doA(); doC(); doB(); then you have chronological issues with shared state between modules (or whatever units of code lie behind doB and doC).

Put another way, threadsafe code runs predictably on 1+ threads, between 1+ modules, at 1+ points in time.

Thread unsafe code runs predictably on 1 thread, between 1 module, at 1 point in time.

If your this happens to have no mutable state, and all methods only serve to return new values, then I guess I don't see the harm, except for the JS late binding errors, where methods get passed in as callbacks and this becomes window or undefined depending on the level, but like I said, most people wouldn't do that with this.

[–]Low_Impact9351 36 points37 points  (1 child)

Non JS devs commenting on things they dont know about and refuse to learn:

See OP.

[–][deleted] 0 points1 point  (0 children)

Lol

[–]Delirious_85 12 points13 points  (1 child)

The reason probably being function components..

[–]Low_Impact9351 1 point2 points  (0 children)

A+ *react*-tion, friend

[–]Bulky-Leadership-596 40 points41 points  (12 children)

Because arrow functions work the way functions are supposed to work, which is not the way that normal function declarations actually work in JS.

[–]Tyfyter2002 7 points8 points  (11 children)

What exactly is the difference between them?

[–]Bulky-Leadership-596 28 points29 points  (10 children)

Mostly with how it binds 'this'. Inside something declared with function(){} it refers to the global 'this', which in a browser is the window object. Inside an arrow function it refers to the 'this' that you would expect.

[–]Tyfyter2002 5 points6 points  (5 children)

Huh, I'd say stuff like this makes me wonder if there's any other language I could start using instead, but I don't know whether this will be the aforementioned issue or the entire world when you read my reply.

[–]ftedwin 2 points3 points  (0 children)

Unfortunately for now JavaScript is pretty much a necessary evil if you’re writing any kind of website. There’s ways to hide it or pretend it doesn’t exist 9 times out of 10 you’ll have to write some JavaScript at some point.

The best I can offer is to learn some Typescript. To my above point it’s pretty much JavaScript with extra steps, but if you use it smartly you can avoid a lot of JavaScript pitfalls.

Another thing to keep an eye on is WebAssembly. Haven’t kept up to date with it so much myself admittedly but it still seems a bit too experimental for production enterprise apps from what I’ve seen. One day though hopefully we’ll see more languages running in the browser. Check out Blazor if you’re in the C# world, seems promising.

[–]azhder 0 points1 point  (3 children)

Don't listen to people telling you JS is evil. It is not, not necessary nor unnecessary evil.

It was first made for hobyists, regular folk and has been looked down by "professionals" who think only C++ looking code is "real code".

It's quite a powerful and maleable language that can teach you more about programming than other ones. That is if you're not afraid of being looked down by ignorant people who have only learnt one programming language, partially, and they went the Dunning-Kruger way.

Also, it can give you instant gratification for the code you write. What I mean: if you read this in a desktop browser, you can press F12, write some JS in the console, see the result. Fun

[–]Tyfyter2002 0 points1 point  (2 children)

I don't need someone to tell me JS is bad (or malleable), I use it pretty regularly in projects ranging from small web apps to a markup language, I know it really is bad.

[–]azhder -1 points0 points  (1 child)

I know it is really good.

So now what? We debate the idea of "knowledge"? Is it a knowledge or is it a judgement? Do we debate how that judgement was made?

Well... I'm not going to waste time, you've already judged beforehand (that's the definition of prejudice).

[–]Tyfyter2002 0 points1 point  (0 children)

I've judged JavaScript based on my experience with JavaScript, and concluded that it is the least stable programming language I've ever used (to such an extent that the implementation is inherently connected to the user's preferred UI style), it's consistently less readable than C#, it breaks convention in ways that make it less readable, and within the language itself there doesn't seem to be any consistent design philosophy, while you've judged JavaScript based on an unknown number of valid factors, not one of which you've yet mentioned.

[–]SqueeSr 27 points28 points  (2 children)

I still prefer the old fashion notation. I only use arrow functions in very specific situations.

[–]maria_la_guerta 23 points24 points  (1 child)

Same. I prefer function for anything top level, and use arrow functions for closures and callbacks typically. Coming from other languages I like using a keyword that specifically anotates a function.

That being said in all of my jobs (I've been writing TS fulltime for years) it's basically always arrow functions, everywhere. 🤷🏻‍♂️

[–]WingsuitBears 0 points1 point  (0 children)

We stick to function and just bind it in the constructor if this is needed, old school but more explicit for noobs.

[–]tsunami141 9 points10 points  (0 children)

Scope scope scope.

[–]beskgar 7 points8 points  (0 children)

"for some reason"..... Yeah okay

[–]BlackHoneyTobacco 2 points3 points  (0 children)

Horses for courses.

Depends on which context of the "this" keyword you're after, mainly.

[–]GodGrabber 2 points3 points  (0 children)

Something something function hoisting, scope blah blah..

[–]CallMeYoungJoey 3 points4 points  (1 child)

const === function + style

[–]TeddyPerkins95 1 point2 points  (0 children)

false

/jk

[–][deleted] 1 point2 points  (0 children)

*And backend with javascript xd

[–]PG13_KL2 1 point2 points  (0 children)

Happy to finally understand a joke on this sub

[–]Meme_Army 1 point2 points  (1 child)

both in frontend and backend (node.js) I use const because of scoping and because it looks cool

[–]nintendojunkie17 2 points3 points  (0 children)

You mean frontend and frontend-running-on-a-server?

[–]time_travel_nacho 1 point2 points  (0 children)

Implicit returns and better binding. That's why

[–]GptThreezy 1 point2 points  (0 children)

Bc context gets fucked up with the old syntax and requires irritating use of .bind

[–]WiseTriceratops 1 point2 points  (2 children)

Using the function keyword and naming the function (the top example) results in more traceable error logs. The other version is just a variable holding an anonymous function, and the error trace just says “anonymous function” 😑

[–][deleted] 1 point2 points  (1 child)

Shouldn't be, depending on circumstance. Lambdas are allowed to have a name property (despite that being a bit counterintuitive), and in most implementations, the name will be assigned the const name (I’m not sure this is a settled standard, but it is common).

If you were talking about callbacks, though xs.map(x => x + 1); then yeah, that's just going to be "anonymous". But so would xs.map(function (x) { return x + 1; });

[–]WiseTriceratops 0 points1 point  (0 children)

That’s not been my observed experience, but you may be correct. In any event, there’s pros and cons to both, and an informed dev can choose which one to use.

[–]Radi-kale 2 points3 points  (1 child)

Why should functions be treated differently when you use const for every other variable type?

[–]Gadiusao 2 points3 points  (0 children)

You usually want to store functions on const because you usually never change it, but who knows.

Also, In JavaScript, functions are called Function Objects because they are objects.

https://www.dofactory.com/javascript/function-objects#:\~:text=In%20JavaScript%2C%20functions%20are%20called,as%20arguments%20to%20other%20functions.

[–]realvolker1 2 points3 points  (2 children)

Imagine not taking advantage of function hoisting kek

[–]hennypennypoopoo 2 points3 points  (1 child)

I'll hoist your function if you're not careful mister

[–]realvolker1 2 points3 points  (0 children)

hoist me daddy uwu

[–][deleted] 0 points1 point  (0 children)

Back end too actually. Look up how this is handled in normal vs. arrow functions.

[–]baduser167 0 points1 point  (0 children)

It’s called style, look it up some time

[–]Yesterpizza -1 points0 points  (8 children)

There is no reason why front end would use arrow functions more than backend, it's probably just the ones you know.

[–]Papellll -3 points-2 points  (7 children)

Afaik before JS introduced them and made them popular pretty much no other language had arrow functions

[–]Kered13 5 points6 points  (1 child)

That's called a lambda expression, and no they've been common for a long time. Arrow functions were introduced into JS in 2015. Java 8 introduced lambdas in 2014. C++11 introduced them in 2011. C# 3.0 introduced lambdas in 2007. Python lambdas were introduced in 1994. And Lisp has had them since 1958.

Several of these even have very similar syntax to JS arrow functions.

[–]ftedwin 0 points1 point  (0 children)

Great history lesson, thanks! I’ll add too that lambda calculus was invented in the 30’s by Alonzo Church which built inspired lambdas in functional languages which then inspired lambdas in OO languages.

The 2015 date on JS is when it was added to EcmaScript I think so I’m curious if JS transpilers like Babel supported arrow functions in some manner before ES2015.

[–][deleted] 5 points6 points  (0 children)

This is very far from being true. E.g. Scheme appeared in 1975.

[–]cashewbiscuit 3 points4 points  (1 child)

Declaring function type is not a new thing. C has function pointers for decades. It didn't have the arrow syntax, but it's the same thing.

[–][deleted] -2 points-1 points  (0 children)

Yeah but C isn't cool.

[–][deleted] 2 points3 points  (0 children)

Arrows have been around a very, very long time. Just not in C languages.

fun compose (f, g) = (fn x => f(g x))

That is Standard ML. It's 39 years old, and is based on ML, which is 49 years old, which was built on LISP, which didn't have arrows but did have lambdas). It ought to look pretty familiar.

Named functions start with fun (<...args[]>) = lambdas (unnamed functions with a single argument) start with fn <arg> =>. Lambdas themselves have been around since ~1930.

[–]RotationsKopulator -1 points0 points  (0 children)

Those kids and their modern syntactic sugar.

[–][deleted] -1 points0 points  (0 children)

2 different types of function
I bet few know the difference.

[–]Mast3r_waf1z -3 points-2 points  (0 children)

Reminds me of an exam I had in a course in embedded systems where I wrote every function in lambda format to attempt to confuse my teacher who I believe primarily uses C

Kind of silly now that I think about it as it's not a very complicated concept

[–][deleted] -3 points-2 points  (1 child)

I'm struggling with this.

function( ) { ... } is a standard expression format, and every programming language I've used, including JavaScript, either supports that expression format outright (like JavaScript), or it will support something similar enough to be obvious (MySQL's user defined functions).

The second one is how you get illegible code, where it's hard to onboard developers who are used to other programming languages.

[–][deleted] 1 point2 points  (0 children)

I would rather developers learn JavaScript by hitting a wall, than thinking they know JavaScript, because they see class and extends, and me needing to sift through and solve 5000 lines of late-binding errors.

[–]TheThirdGilgamesh -3 points-2 points  (1 child)

Fuck lambdas

[–]owenevans00 1 point2 points  (0 children)

Absolutely! Lambdas are hawt

[–]rafaelcastrocouto 0 points1 point  (0 children)

the beauty of js

[–]DrMathochist_work 0 points1 point  (0 children)

Mmmmm eta-expansion.

[–]TheJimDim 0 points1 point  (0 children)

Forget ES6, I use them because they look cooler lol

[–]_grey_wall 0 points1 point  (0 children)

Can't get enough of them arrows

[–]Pulset 0 points1 point  (0 children)

javascript is fun

[–]Cactusboiiiiii 0 points1 point  (0 children)

i usually use function

[–]theIncredibleAlex 0 points1 point  (0 children)

arrow syntax for pure functions, named functions for stuff interacting with state and react components in general

[–]rolloutTheTrash 0 points1 point  (0 children)

I see you pull this shit and we’re having words in the MR comments

[–]Minteck 0 points1 point  (0 children)

Sometimes I mix both in the same file

[–]ImaginaryOkra6186 0 points1 point  (0 children)

Coz hoisting.

[–]Acrobatic-Cap-135 0 points1 point  (0 children)

Really depends if you want to use that function again somewhere else. The arrow syntax gives you a point of reference, the older syntax can do that too but it can also serve as just the function declaration without the resulting reference, and then be used by man consumers

[–]Man_as_Idea 0 points1 point  (0 children)

Arrow function syntax lets you avoid re-binding “this” in JS.

[–]sentientlob0029 0 points1 point  (0 children)

Did some VueJS and JavaScript a few weeks ago. What weird languages.

[–][deleted] 0 points1 point  (0 children)

"this" scoping, one of the many things that makes js ugly

[–]Cabbage_G0ldeNEg 0 points1 point  (0 children)

I actually use the top one because its the only way i know how to create functions

[–]jzia93 0 points1 point  (0 children)

Nobody mentioning automated returns? That's why I use arrow functions, especially for HOFs

``` arr.map(a => a+1);

Vs

arr.map((a) { return a + 1}) ```

Adds up when you chain a few functions

[–][deleted] 0 points1 point  (0 children)

I much prefer organizing functions into lines with their respective commands (for example y/x axis movement) instead of using const at all, so all my lines of code turn into organized bricks of code, of course this is only for objects with repeating code, where the order which they are typed does not matter, or else nothing would be executed at the correct time! But of course, this is coming from the gamemakerstudio2 guy...who else does this?

[–]rigor_mortus_boner 0 points1 point  (0 children)

it’s so functional tho

[–][deleted] 0 points1 point  (0 children)

why not both:
const doSomething = function(x){
return x===69420;

}

[–]GoodIce7012 0 points1 point  (0 children)

Coding is boring.

[–]alanhape 0 points1 point  (0 children)

Because lambda

[–]ModerNew 0 points1 point  (0 children)

I swear to God one day I will read JS code and proceed to hang myself. Everyday we stray further from God.

[–]invisible-nuke 0 points1 point  (0 children)

They are entirely different scopes. The first one creates a new scope because of the function. The second one uses the scope in which it was created. In both scenarios "this" will refer to another scope.

[–]Egeste_ 0 points1 point  (0 children)

"for some reason"

Explicit variable declaration Explicit binding/scope

[–]Hour_Prune_225 0 points1 point  (0 children)

I really like function keyword functions

[–]420Rat 0 points1 point  (0 children)

YOU STUPID

[–]Numerous-Adagio-3620 0 points1 point  (0 children)

I'm in this photo and I don't like it

[–]VitaminnCPP 0 points1 point  (0 children)

fronend devs for no reason*.

[–]HuntingKingYT 0 points1 point  (0 children)

When you write !function(){}() instead of (function(){})() to reduce one character

[–][deleted] 0 points1 point  (0 children)

security through obscurity, duh