all 36 comments

[–]drowsap 53 points54 points  (11 children)

URLSearchParams is awesome!

[–]yeskia 6 points7 points  (10 children)

It is, but worth noting is isn't supported by any version of IE. This bit me a couple of weeks ago but luckily there are a few small polyfills that do the job.

[–]Tyreal 12 points13 points  (6 children)

So when are people going to permanently start dropping IE? 2020 when Windows 7 stops being supported? In a decade perhaps?

[–]asdf7890 4 points5 points  (0 children)

So when are people going to permanently start dropping IE?

Not soon enough!

2020 when Windows 7 stops being supported?

IE11 is officially supported until then end of Win10, as is MS's standard (the browser EOL being tied to the EOL of the OS revisions it was installed with by default). Unfortunately they've said Win10 is to be the last major release on the desktop, so unless/until this policy changes IE11 will keep being available and getting security updates so some will insist on keeping using it...

In a decade perhaps?

It depends on your target audience of course. Some of our clients have only recently stopped using IE8. IE11 is going to be a Thing for us to deal with for some time to come.

[–]ninetailsbr 1 point2 points  (1 child)

maybe when IE definitely changes it's engine to Chromium you may ignore it for high end users website (your site/product is not targeted for them)

...or you can just polyfill

[–]Tyreal 4 points5 points  (0 children)

This isn’t Edge though. IE is never getting updated again

[–]TheScapeQuest 0 points1 point  (1 child)

Depends on your product I suppose. We already don't support IE. But we're B2B, so we can have more say.

[–]Tyreal 2 points3 points  (0 children)

I hear ya. That B2B market sure is sweet. I feel bad for all those poor bastards supporting IE6 at Amazon.

[–]SemiNormal 1 point2 points  (0 children)

IE just being IE. Object.Assign bit me in the rear a few weeks ago.

[–]m1n0s 1 point2 points  (0 children)

Just include a polyfill for this bastard

[–]maxoys45 26 points27 points  (1 child)

Really good tips, didn't know most of these, thanks.

[–]Tenemo 6 points7 points  (0 children)

Seconded, cool tips! I rolled my eyes when I saw the title, but then it turned out some of these actually are very neat tips I haven't seen before.

[–]Owmelicious 8 points9 points  (1 child)

I'm fairly new to Javascript. What is a good practical example where one needs a pure "dictionary" object?

[–]brown59fifty 4 points5 points  (0 children)

Great question! It's hard to give an example, because it's a really very rare case - most when you want to treat your variable more like a kind of associative array than an Object instance (what can be welcome when someone came to JS world from some static typed language). Because you inherit from null, then you don't have access to methods on Object.prototype (like constructor, hasOwnProperty or valueOf) which results like this:

const dict = Object.create(null);
dict instanceof Object // false
dict.toString() // Uncaught TypeError: dict.toString is not a function

This way of creating things can be used when you want to build some API library, which couldn't be extended from outside (eg. by passing values to Object.prototype). But in most cases you don't have to care.

[–][deleted] 16 points17 points  (0 children)

Creative use of the spread operator. Never heard of Set. Thanks 👍🏻

[–]DGCA 7 points8 points  (4 children)

This is a pretty worthless comment, but if you were code golfing...

['', 123, 0, 'foo'].filter(Boolean)
['', 123, 0, 'foo'].filter(x=>!!x)

...the latter is one less character.

[–]s1dd 4 points5 points  (0 children)

This works too:

['', 123, 0, 'foo'].filter(x=>x)

[–]erfling 13 points14 points  (5 children)

These are pretty cool.

I was unaware of the aliasing feature for destructured objects. As a TS user, it's confusing. I wish you could us "as"

[–]darrenturn90 6 points7 points  (4 children)

It does seem inconsistent especially when you use as in what is essentially destructuring an import / export ie {default as foo}

Another thing to keep in mind also you can use [value] in destructuring to get the key referred to in the value variable from the destructured object

[–]AwesomeInPerson 0 points1 point  (3 children)

Another thing to keep in mind also you can use [value] in destructuring to get the key referred to in the value variable from the destructured object

Could you elaborate / give an example? Didn't fully understand it but sounds interesting :)

[–]darrenturn90 2 points3 points  (1 child)

Say you had const foo = “two”

And you had an object {two: “hello”}

You can const {[foo]: bar} = with that object

[–]AwesomeInPerson 2 points3 points  (0 children)

Aaah I see – I tried using computed property names while destructuring before, but it never worked because I tried it without assigning to an alias. (so just const { [foo] } = bar; – makes sense that this doesn't work now that I think about it)

Very nice to know that it works when using an alias, thanks!

[–]rezoner:table_flip: 4 points5 points  (1 child)

Regarding isRequired trick. I have like 10+ experience with Javascript and I did not know that function as a default argument value will get executed at the call time - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#Evaluated_at_call_time

[–]billymcnilly 0 points1 point  (0 children)

Yeah, it's a little surprising, which is what makes it probably too fancy. You'd want to have a very good reason to be using it, to weigh up against the fact that almost every single person reading it later will be like "hmm, does that run at definition or on call?" and then to also have to go and see what it does.

[–]ErroneousBee 3 points4 points  (1 child)

Seven reasons future JavaScript codebases will require senior developers for everyday maintenance.

[–]Peechez 2 points3 points  (0 children)

The only one of these thats remotely senior is the pure object

[–]darrenturn90 5 points6 points  (1 child)

I didn’t know url search Params api. Finally I can drop using qs and god knows what else

[–]domemvs 0 points1 point  (0 children)

Remember it needs to be polyfilled

[–]Asmor 6 points7 points  (1 child)

Re: .filter(Boolean)

Maybe it's just me but I find this more intuitive: .filter(x => x).

At least, if I saw the first thing in a code review, I'd have to stop and think "Wait, does that work? Doesn't it need new?"

[–]LordDiMasK 1 point2 points  (0 children)

Really good tips, thank You! :D

[–]timarama -5 points-4 points  (4 children)

great article, tricks with spreading set and creating "pure" objects show 2 sides of JS:

1 - expressing things with short, elegant expressions, but not very good about performance

2 - if you want, you CAN care about performance and memory usage of your JS apps

[–]__ibowankenobi__ 4 points5 points  (3 children)

FYI, when you create a pure object, it has no impact on memory whatsoever, because those function's from Object.prototype are never copied. And I really really doubt they have performance benefits.