I hand-picked some of the most useful code snippets from 30 seconds of code. It's an awesome resource, go ahead and show it some ❤️.
In this artic...
For further actions, you may consider blocking this person and/or reporting abuse
hasClass(document.querySelector('p.special'), 'special');
doesn't make sense. If you're already searching with the class in selector, couldn't you just doif (document.querySelector('p.special'))
?To point 12.
Instead of confusing reduce I would do rather:
const formToObject = form => Object.fromEntries([...new FormData(form)])
(when Object.fromEntries() is available indeed...)
Congrat, you reinvented
yarn add jquery
I didn't know about the classList.toggle. Thanks.
In nodejs there is native package called querystring for url parameters.
I find it useful copy to clipboard, used it in a project.
That number 14 really doesn't do anything. You might as well just assign the function to
delay
.I see your point. This is great for hard-coded values in setTimeout() method and without need to create other constants later, like:
by wrapping setTimeout() method in a function you can re-use your defined timer function with different values each time without creating new constants:
I'm not sure I understand what you're saying here. They behave the same.
Personally, if I were to wrap
setTimeout
it would be to change the function signature, something like this.That's the point - they behave the same, but you don't have to create new variables for the same functionality throughout your app. Instead you just use the same function once you need timer functionality for something.
Furthermore, if you need different function signature, you can use the latter assigning it to different var.
if I assign
setTimeout
that will force me to create new unnecesary variables in the entire app? And that doesn't happen if I wrap it in a function?The
getURLParameters
is not really safe/useful, I'd go instead with:or, for possible duplicated keys:
To to check if the element specified is visible in the viewport,
IntersectionObserver
is more performant and available in all major browsers.Thanks for your feed. Checked in caniuse and the support is better than i expected :)
I love one liners but I'm afraid that my co-developers wont get it right.
For those not familiar with ES6 syntax it could be harder to grasp at first :)
Yeah, but these aren't one liners in the traditional sense (code crushed like a soda can for no good reason). The new syntax is worth working to understand since it brings benefits beyond brevity.
22 can be shortened a lot. Here is shorter version.
Thanks for your point. I first started with clipbard.js for that (3kb gzipped) :)
This is wrong! ^
This is correct! ^
These are the worst piece of advice I saw, sorry, I'm just being honest. You have toggle already, you wanna wrap that, just use the original method, why not use setTimeout instead of delay? Use fetch to post to a url, just use remove event listener to remove, following these, people will have to learn new functions instead of using normal Javascript
Some of them might be useful, but I found most of them to be nonsense.
Why do you need the spread in the first example, rest params will already be collected into an array
Thanks for posting this! I'll definitely keep them saved and in mind. I see that a lot of people are saying some comment about how something isn't best practice, but I'm happy you even posted this. Even if there are better ways, most of these are great examples and approaches I would've never thought about on my own.
To point 11. It is maybe modern JS but there is URL object for these things for some time, no need for regexping
You have all the parameters in URL searchParams object
const params = new URL(window.location.href).searchParams
You can just spread it into entries
[...params] or [...params.entries()]
or if you want to have object so much (even it makes not much sense here, since you can have valid params string with multiple same parameter with different values and then you will loose them except last one, since object cannot have multiple same keys...)
Object.fromEntries([...params])
Thanks for the blog, some very nice solutions to problems I bump into regularly!
This is great..Thanks
You don't need to do
[...el]
I think in the first one. Is there any reason you do that?