TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JS Adolescence: Change of opinion regarding certain JavaScript practices

39 pointsby niyazpkover 12 years ago

9 comments

jrajavover 12 years ago
Repeatedly used inline object literals:<p>Object literals are one of the best features of Javascript, learn to love them. In the example cited, you already have a function dedicated to creating this object; why would you need to further obfuscate it by delegating the actual leg work to a buzzword-filled framework (or worse, vanilla constructors)? Even for more complex examples, I find it clearer and simpler to use an extend() or clone() method than something that tries to emulate classes.<p>Strict equality everywhere:<p>You cherry-picked two simple examples, but normally it's more complex than that. Javascript has rather odd coercion, and it will bite you if you try to memorize all the cases where it's safe to use ==. It's not really that === is "strict", it's just that it generally works how == was supposed to. More importantly, though, there's never any harm in using === instead of ==, so why would you use the latter? Why is === "overkill?" Because it's one character more? I fail to see the argument for not using it, except to play the devil's advocate.<p>Strict coherence to a standard:<p>This is a fine swing to take if you're doing hobby work, in fact it's very healthy, but if you're working on any sort of larger project where you are or will be collaborating with others, it is nothing short of essential to agree on standards for everything from brace style to camel case to testing frameworks. Almost every other argument concerning style, practices, and idioms falls apart in favor of consistency.
评论 #4782270 未加载
pagsover 12 years ago
Why post this stuff? He offers opinion without explanation, this is the same type of JavaScript "wisdom" that probably had him following the conventions he is now nixing in the first place. I'm not sure if his "dogma" disclaimer at the end is ironic or a cop-out.
评论 #4782042 未加载
评论 #4782272 未加载
btiplingover 12 years ago
I disagree with abandoning one statement var declarations. This will bite you. A variable declared with var is in scope for the entire function because of hoisting.<p>It's also just bad form because of how people write Javascript, for example if you have an unfortunately long nested function with a variable declaration toward the bottom with a common name, say 'width' and later add that variable name a level up in scope and then think to use it in your function because you forgot or didn't know you had a var statement declaring width somewhere in that function you'll have fun staring into the abyss when this runtime error eventually presents itself.
评论 #4782101 未加载
Xcelerateover 12 years ago
I disagree with not using "inline object literals". They're basically maps, and in fact some programming languages (e.g. Self) are built around the concept of using maps instead of a list of arguments. I think in some cases this can be beneficial. Much more so than SomeWeirdCall(0, 0, 0, 0, 0, 0, NULL, "some data");
fourstarover 12 years ago
Aside from inline object literals, and using strict equality (which you should always be using IMO), what does this specifically have to do this Javascript? Everything else applies to general programming.
评论 #4781785 未加载
评论 #4781656 未加载
评论 #4781694 未加载
richforresterover 12 years ago
Funny; I comment the living carp out of my JS.<p>Mainly because I suck at it (I'm a designer, not a developer, dagnabbit) and it helps me keep track of what I'm doing.<p>Other than that, great article, taught me a thing or two.
kondroover 12 years ago
Disagree vehemently with the idea that long variable names equate to bad API design.<p>The more descriptive your naming, the better your design and the less comments you require.
评论 #4782162 未加载
ronaldjover 12 years ago
Can someone explain how this is potentially dangerous?<p>if (!cachedReturnValues[key]) { cachedReturnValues[key] = value; }
评论 #4782350 未加载
calinet6over 12 years ago
Here's a tip: you're still an adolescent. Come back in 5 years.