Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
In what world did arrow functions make it worse. They make sure that the lambda captures this in the only way that makes sense (e.g. if we're in a class, this refers to the object)

Adbot
ADBOT LOVES YOU

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

Joda posted:

In what world did arrow functions make it worse. They make sure that the lambda captures this in the only way that makes sense (e.g. if we're in a class, this refers to the object)

By adding to the mental load of remembering how the this keywords works in every context. I agree that functionally the way this in arrows works is useful, I could have phrased that more clearly, sorry.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
That's true. It adds another dimension to a keyword that already has too many meanings. Tbh I never actually used it until ES6 and TS when it started having the same function as in any other OO language.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Joda posted:

That's true. It adds another dimension to a keyword that already has too many meanings. Tbh I never actually used it until ES6 and TS when it started having the same function as in any other OO language.

the arrow function changing the scope is a super-useful feature, but the issue is that arrow does so much, and it is relatively difficult to google. If Jimmy hasn't looked up what it does, it's entirely possible he will miss the scope implication of using the arrow function, and that'll take him down a rabbit hole for a few hours.

akadajet
Sep 14, 2003

Bruegels Fuckbooks posted:

the arrow function changing the scope is a super-useful feature, but the issue is that arrow does so much, and it is relatively difficult to google. If Jimmy hasn't looked up what it does, it's entirely possible he will miss the scope implication of using the arrow function, and that'll take him down a rabbit hole for a few hours.

Any decent JavaScript introduction will take the time to explain this. So long as Jimmy isn't just throwing poo poo at the wall to see what sticks and doing some research about the language he is trying to use he should be fine.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

akadajet posted:

Any decent JavaScript introduction will take the time to explain this. So long as Jimmy isn't just throwing poo poo at the wall to see what sticks and doing some research about the language he is trying to use he should be fine.

Have you met 80% of software developers??

akadajet
Sep 14, 2003

Lumpy posted:

Have you met 80% of software developers??

The guys I pass on when interviewing? Yes.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Joda posted:

That's true. It adds another dimension to a keyword that already has too many meanings. Tbh I never actually used it until ES6 and TS when it started having the same function as in any other OO language.

That never happened, though?

I mean, I know they added some syntactic sugar to make it look a lot like the class-based inheritance people are more used to - even with the class keyword, but it doesn't. It's a trick to get you to think that if you perform this ceremony with the class and constructor keywords, the language is suddenly doing class-based inheritance but it's not. It's still prototype-based and you still have to bind methods that care about having the class's this available.

The football is class-based inheritance in JS, you're Charlie Brown and the new syntax is Lucy.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Munkeymon posted:

It's still prototype-based and you still have to bind methods that care about having the class's this available.
For languages that are class-based you still have to bind a 'this' to a function you're referencing like a function pointer. In C++, for example, you have to call a member function pointer with an explicit this.
Any language that *doesn't* require you to do it is still just doing it for you in secret. The difference with Javascript is that it won't tell you you're doing it wrong, it'll just do the wrong thing.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

akadajet posted:

The guys I pass on when interviewing? Yes.

dude if you've never had a back-end developer who doesn't give a gently caress about front end / indian contractor / new kid out of school ever have to do a minor update to a UI... like, it's probably the least damaging thing that they could possibly be working on.

smackfu
Jun 7, 2004

I know we have a lot of self taught JavaScript people who used to do Java and their understanding of JavaScript complexities is perhaps... lacking.

Tea Bone
Feb 18, 2011

I'm going for gasps.
I'm pulling my hair out over this.

I have a selection box, depending on it's selection it should make the input in a following div disabled.

html:
code:
<select class="my-selection">
	<option value="1">One</option>
	<option value="2">Two</option>
</select>
<div class="input-container">
	<input type='text'>
</div>

Then the java script:

code:
$('body').on('change','.my-selection',function(e){
        if ($(this).val() === '2'){
            var content= $(this).nextAll('.input-container').first();
	    foo = content.find('input')
            content.find('input').prop('disabled', true);
        }
    });
For the life of me I can't get it to work. I can call .hide() on the input and it works and the really odd thing is, if I run foo.prop('disabled', true) from the console the input disables as expected. I'm guessing I've misunderstood how find/nextAll work.

edit: Never mind, I'm an idiot who had another function further down which enabled all inputs on form change.

VVV Thanks for checking anyway.

Tea Bone fucked around with this message at 16:29 on Nov 13, 2018

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

Tea Bone posted:

I'm pulling my hair out over this.

I have a selection box, depending on it's selection it should make the input in a following div disabled.

html:
code:
<select class="my-selection">
	<option value="1">One</option>
	<option value="2">Two</option>
</select>
<div class="input-container">
	<input type='text'>
</div>

Then the java script:

code:
$('body').on('change','.my-selection',function(e){
        if ($(this).val() === '2'){
            var content= $(this).nextAll('.input-container').first();
	    foo = content.find('input')
            content.find('input').prop('disabled', true);
        }
    });
For the life of me I can't get it to work. I can call .hide() on the input and it works and the really odd thing is, if I run foo.prop('disabled', true) from the console the input disables as expected. I'm guessing I've misunderstood how find/nextAll work.

Works for me https://jsfiddle.net/voa2jmeq/

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



roomforthetuna posted:

For languages that are class-based you still have to bind a 'this' to a function you're referencing like a function pointer. In C++, for example, you have to call a member function pointer with an explicit this.
Any language that *doesn't* require you to do it is still just doing it for you in secret. The difference with Javascript is that it won't tell you you're doing it wrong, it'll just do the wrong thing.

Is it that common to call members functions/class methods as function pointers from other methods in the same class in C++?

And, yes, that's the rub: they made new syntax that looks like it does in other popular languages that automatically bind class methods to the object but you have to just know that that's still not happening and do it yourself manually. Really, they should have used prototype instead of class just to clarify that it's really an object with an optional initializer that makes prototype chaining less verbose.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Munkeymon posted:

Is it that common to call members functions/class methods as function pointers from other methods in the same class in C++?
Not any more, because now you'd generally just lambda it up, like you do if you're doing it right in JS. :)
But before lambdas, yeah, maybe not common, but not uncommon.
Edit: common enough that "member function pointer" is a thing at all. It's not like they have much other purpose.

Rand Ecliptic
May 23, 2003

Jesus Saves! - And Takes Half Damage!!
Is there a general consensus on the best JS books? I spend a lot of time on the train without wi-fi, so I'd like some books to refine my knowledge.

I've heard good things about the You Don't Know JS series... anyone have experience with these?

huhu
Feb 24, 2006
I've been developing in JavaScript for about 4 years now. I started learning it via jQuery and originally hated the language because I could never really understand jQuery. Moved from jQuery to React and along with learning all the new ES6 syntax, I really feel like I finally know JavaScript. I've been a full stack JS dev with Express/React for the past 6 months as well. With the background out of the way, I'm looking at building a toy project in Electron and I just worked my way through an intro tutorial. Here are my questions:

1. Is it worth it to learn "Vanilla JavaScript"? I feel like at this point I know a solid amount of stuff about JS. In the Electron tutorial I understand selectors and such but I'd much rather work with JSX and JS than deal with the actual intersection of HTML and JS.
2. Are React and Electron together a good idea? I'm looking through some tutorials now and I can't tell if React in Electron is a typical choice or if it's just something that people who know React do.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

huhu posted:

1. Is it worth it to learn "Vanilla JavaScript"? I feel like at this point I know a solid amount of stuff about JS. In the Electron tutorial I understand selectors and such but I'd much rather work with JSX and JS than deal with the actual intersection of HTML and JS.
I've not done React, but I went from jQuery to vanilla and find it less irritating because it doesn't have secret sauce behind everything you try to do, it just does what you ask it to. Though there are occasional irritations like checking whether something is 'visible' (ie. not hidden, not display:none, not size 0) is weirdly awkward with vanilla JS and there's a function for it in jQuery.
I know other people who mostly use React and find it a relief when they're allowed to just use vanilla JS.
A strong benefit of not using some framework is that vanilla JS seems much less likely to get changed out from under you and force you to rewrite a bunch of stuff. But maybe I've just been super unlucky with being burned that way literally every time I use any kind of framework for anything.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice
One of the things I like about React is that most of your code is vanilla JS, so you’ve already picked up a lot. As for Electron and React, I don’t see why it would be a bad thing... React is a framework for front end views, so seems like a good fit. If you are using Redux or some other state manager, they are designed to manage app state, regardless of if it’s a web page or whatever.

Roadie
Jun 30, 2013
If you use React in its simplest form, with stateless components for everything, it's fundamentally just a way for mapping a big set of nested functions to HTML output and getting high-performance automatic updating when you change the function inputs. The only weirdness to really watch out for is that the props are named after JS DOM properties, not the actual HTML attributes.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I don't know if I'm helping or not but my experience is that Vanilla is the best Javascript environment of all time. If it's in node I don't have to compile anything, as few dependencies as absolutely humanly possible, no nothing, it's more or less zen. As zen as Javascript can be.

For frontend you have to build for es5, I prefer really strongly the simplest tools possible. So buble for basic es5 sans IIFE, and then rollup to put that es5 with es6 imports into a single file. If I'm doing the build step then there's no reason not to basically use Vue (or React) really. If it makes sense and I'm not just using a whole bunch of tools when it's not necessary.

Wish I could use Vue with buble/rollup instead of webpack or similar colossal monstrosities that do way more than necessary. :(

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
Why do you have to compile for ES5?

necrotic
Aug 2, 2005
I owe my brother big time for this!
Browser compatibility?

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
Are there any browsers except IE that haven't updated to at least ES6? Like sure, you might have to support that, but it's definitely not a general imperative as stated

necrotic
Aug 2, 2005
I owe my brother big time for this!
Edge still has some warts, too. It would be great to target ES6 but it's still difficult when you need to support IE or Edge.

necrotic
Aug 2, 2005
I owe my brother big time for this!
The need for roll-up or something similar is still pretty big, too. Which means at least replacing import statements with something else.

MrMoo
Sep 14, 2000

Joda posted:

Are there any browsers except IE that haven't updated to at least ES6? Like sure, you might have to support that, but it's definitely not a general imperative as stated

Embedded hardware is still ES5 only, surprisingly all the digital signage from Samsung and LG are circa Safari 6.0 level browser tech.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I would say if you are compiling anyway you should compile for maximum compatibility, if there are no significant drawbacks to doing so.

TIP
Mar 21, 2006

Your move, creep.



MrMoo posted:

Embedded hardware is still ES5 only, surprisingly all the digital signage from Samsung and LG are circa Safari 6.0 level browser tech.

Based on my recent testing, Safari 6 doesn't even support all of ES5. I was doing some testing on an iPad 2, which I think was Safari 9, and it required ES5 polyfills to get my code working.

I loving hate Safari.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Nolgthorn posted:

I would say if you are compiling anyway you should compile for maximum compatibility, if there are no significant drawbacks to doing so.

Don't you lose significant performance potentially? Also I assume there is no way to make wasm compatible with an ES5 platform

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Joda posted:

Don't you lose significant performance potentially? Also I assume there is no way to make wasm compatible with an ES5 platform

i haven't run into a case where compiling back to es5 hurt performance short of "oh, there are more characters so it'll increase the load time of the page."

emscripten can compile to either asm.js or webassembly, so you can provide both a web assembly version and an asm.js. asm.js can run in IE11 or Safari. the load times are a lot better for webassembly and IE11/Safari didn't run the asm.js stuff nearly as well as chrome did, but it might be possible to use it for simple use cases.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
I assume the entire point of using wasm proper (near native performance and threading) is lost if you compile to asm.js?

E: although I guess it still counts as technical compatibility

Joda fucked around with this message at 17:33 on Dec 1, 2018

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Joda posted:

I assume the entire point of using wasm proper (near native performance and threading) is lost if you compile to asm.js?

E: although I guess it still counts as technical compatibility

it depends on your use case. even the asm.js demos can be pretty impressive from a performance perspective. the big issues with asm.js are:

a) There isn't a great way of telling how much memory you're using with asm.js
b) Initial load times

If we want to compare web assembly vs. asm.js performance.
https://blogs.unity3d.com/2018/09/17/webassembly-load-times-and-performance/

Webassembly has much better load times. The asm.js computational performance was already pretty good. Webassembly is an improvement but asm.js was already pretty good in that regard.

Chas McGill
Oct 29, 2010

loves Fat Philippe
Say I want to gather the first 6 results from a YouTube API query and render them as embedded videos on a page, then update those results automatically once a week, what would the simplest approach be?

I thought about just doing the request onload, but I expect someone could refresh the page a lot and fill up my API limits.

huhu
Feb 24, 2006

Chas McGill posted:

Say I want to gather the first 6 results from a YouTube API query and render them as embedded videos on a page, then update those results automatically once a week, what would the simplest approach be?

I thought about just doing the request onload, but I expect someone could refresh the page a lot and fill up my API limits.

Write a cron job to run once a week to fetch new data and store that in your database then just serve whatever's in there with the request.

Whatever you do, don't store you're API key in the frontend code.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Joda posted:

Don't you lose significant performance potentially? Also I assume there is no way to make wasm compatible with an ES5 platform
If you are building a video game in Javascript perhaps you should take performance into account. It might have an effect on performance. But modern computers, even really cheap ones can perform billions of operations per second, most of your processing is going to be consumed with draw calls.

Met somebody once, I was conducting their interview, and they wrote all of their loops in reverse.

code:
for (let i = 10; i >= 0; i--)
So I asked him why he was doing that, he said it was more performant. I tested it, and it wasn't. But even if it was, so what?

I can definitely appreciate having more performant applications. But you do that these days by limiting the number of dependencies you import, eliminate the bulk introduced by "general purpose" tooling. Don't write the code itself in weird ways for an imperceivable <0.1 ms performance boost.

Compiling to ES5 feels that way to me. There's no reason to be worried about reduced performance because of that alone, if there's performance gains to be made probably a good start is not using bootstrap for example. Or replacing javascript animations with css animations. Not using lodash' functional methods. Removing jQuery. That sort of thing.

Nolgthorn fucked around with this message at 20:09 on Dec 2, 2018

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
A lot of my hobby projects are games and games systems, so that's sort of the head space I'm in, but I haven't actually played around with web based game dev yet.

quote:

for (let i = 10; i >= 0; i--)

A lot of people are still stuck in the mindset that they have to do these sorts of micro-optimizations by hand, when the compiler or interpreter or whatever is way more clever than you and is easily able to identify this sort of thing. As long as you're not looping through a linked list that is not continuous in memory, chances are you're not breaking your cache or anything like that, especially in a language like JavaScript.

Where I'm coming from it was more a concern with how the transpiler might use an alternative to an >=ES6 thing that wasn't optimised, or how the JS engine might be able to make some assumptions about your data based on the core lib functions you're using, and using those assumptions to optimize the run-time.

Joda fucked around with this message at 20:14 on Dec 2, 2018

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I'm not joking when I say that in several of my projects, I replaced for(let foo of bar) with for(let i = 0; i < bar.length; i++) let foo = bar[i]; to a significant performance boost. This was after I profiled and saw it was an issue, and then fixed it.

This stuff matters, it really does. The compiler can often do a really crap job with some of this stuff.

Chas McGill
Oct 29, 2010

loves Fat Philippe

huhu posted:

Write a cron job to run once a week to fetch new data and store that in your database then just serve whatever's in there with the request.

Whatever you do, don't store you're API key in the frontend code.

Hmm I think I need a new host. Cheers for the advice.

Adbot
ADBOT LOVES YOU

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Suspicious Dish posted:

I'm not joking when I say that in several of my projects, I replaced for(let foo of bar) with for(let i = 0; i < bar.length; i++) let foo = bar[i]; to a significant performance boost. This was after I profiled and saw it was an issue, and then fixed it.

This stuff matters, it really does. The compiler can often do a really crap job with some of this stuff.

Wait, really? You'd think the array iterable if anything would be optimised in the engine

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply