Live data from Hacker News

Tips to Become a Better JavaScript Developer

justinchmura.com

31–40 of 46 posts

Re: Tips to Become a Better JavaScript Developer

#31

Don't think of "this" as having weird rules with strange edge cases about what it defaults to in certain scenarios. It is always dependent on how you call the function, and there are only two rules, depending on your situation. 1) "this" is whatever you set it to with call/apply/bind, or, if you didn't, 2) if you're using "use strict" 2.a) "this" is whatever is to the left of the dot when you call the function. 2.b)…

[deleted]

Re: Tips to Become a Better JavaScript Developer

#32

Second example in tip #2 is dead wrong. JavaScript does NOT do Dymanic Scoping. I suspect he didn't even bother to run the code. His example throws an error when I try it: "ReferenceError: x is not defined" (as expected). blind-leading-the-blind etc.

While you're correct that the example is incorrect, technically speaking there are places where javascript does do dynamic scoping. Namely in a "with" or with "eval".

Re: Tips to Become a Better JavaScript Developer

#34
post #12

"You can write your own basic jQuery by doing this: var $ = document.querySelectorAll; Obviously this won’t cover everything, [...]" wat

It's not even right for what the article author is trying to do. Or in other words, s/he just broke their own rule #1: understand "this". "document.querySelectorAll" must be called on the document object, and reassigning it to a variable like this will make it be called in whatever function scope it's currently in. To even get this to call correctly, it needs to be "var $ = document.querySelectorAll.bind(document);"

>> "document.querySelectorAll" must be called on the document object, and reassigning it to a variable like this will make it be called with "this" assigned to the global object, or "undefined" if you're using "use strict"

fixed.

Re: Tips to Become a Better JavaScript Developer

#35
post #23

Earlier quoted context omitted.

The actual quote was: >>What I’m saying is don’t rely heavily on jQuery, or any other library, without having some sort of understanding of how that library is accomplishing it’s tasks. Which I think is fair. (However, I don't agree with his assertion that using vanilla JS will be more efficient - in most cases, it won't.)

Vanilla JS is more efficient than jQuery a lot of the time - for example, consider the $(...) api - this has to do string parsing before it makes its selector call, which by its very nature take away computing power. These days it is negligible though. The argument for using jQuery for DOM manipulation is oftentimes just one of readability.

IIRC jQuery started out using string parsing to achieve this but its widespread use prompted browser vendors to add the document.querySelector and document.querySelectorAll methods to their browsers.

Probably in older browsers it still uses the old method but for modern browsers it just passes [selctor] in $("[selector]") through document.querySelectorAll; no string parsing necessary.

Re: Tips to Become a Better JavaScript Developer

#37

I dislike when people say that good developers don't use libraries like jQuery. The main reason I use them is because I'm lazy. I'd much rather write $("#id") than document.getElementById("id"). I rarely write any javascript code that is so resource constrained that the extra time added by jQuery is not acceptable.

I'm guessing people don't mean that if you used jQuery you are a bad developer. Just that, if you are a good developer, you don't rely on it and can write the vanilla version if needed.

EDIT: In this case, I'm wrong, the author seems to think you should write vanilla when given the option which is wrong for a lot of reasons.

Re: Tips to Become a Better JavaScript Developer

#38
post #23

Earlier quoted context omitted.

The actual quote was: >>What I’m saying is don’t rely heavily on jQuery, or any other library, without having some sort of understanding of how that library is accomplishing it’s tasks. Which I think is fair. (However, I don't agree with his assertion that using vanilla JS will be more efficient - in most cases, it won't.)

Vanilla JS is more efficient than jQuery a lot of the time - for example, consider the $(...) api - this has to do string parsing before it makes its selector call, which by its very nature take away computing power. These days it is negligible though. The argument for using jQuery for DOM manipulation is oftentimes just one of readability.

I think by "efficiency" the author meant developer efficiency, as in writing more code in less time. I may be wrong though. You're definitely right that vanilla JS is often times more efficient than jQuery.

Re: Tips to Become a Better JavaScript Developer

#39
post #27
post #11

Tip #4 Ditch the jQuery Crutch: "If you can accomplish the same thing by using vanilla JavaScript, 90% of the time, it’s more efficient to do so." No. Just no. It is not more efficient to do so 90% of the time. jQuery is mature and battle proven. It is already doing what it can do with an efficient way, which comes with years of experience. Saying, write your vanilla javascript and ditch jQuery, your implementation w…

The statement about jQuery being more efficient may have been true about some handrolled for loop selector engine in ie7, but modern browsers have the tools we need now. You can replace most uses of jQuery by aliasing querySelector to $ and querySelectorAll to $$. You lose some of the automatic array iteration stuff, but really, if you were writing good code you would know how many elements you are about to select. j…

Downvote away, but if you've got to load 90kb to alias $, damn.

Re: Tips to Become a Better JavaScript Developer

#40

I dislike when people say that good developers don't use libraries like jQuery. The main reason I use them is because I'm lazy. I'd much rather write $("#id") than document.getElementById("id"). I rarely write any javascript code that is so resource constrained that the extra time added by jQuery is not acceptable.

Laziness is one thing, but jQuery covers so many browser-specific issues and differences that it would be insane to cover it all by yourself. Sure, if you have a simple script with several lines of code than you can go without jQuery, but anything more complex - no way, you will produce 10x more code with 10x more bugs. The only argument against using jQuery could be its size, but let's be honest - nowadays, unless you do something targeted at users with very low connections, it doesn't matter, even on the mobile.
Post reply on HN