6 ms·
Tries to play it cool by wrecking jQuery? Check. No semicolons? Check. Put together by Paul Irish? Check. Yep, this submission is Hipster 1.0 Certified.
by omegote 11y ago
Tries to play it cool by wrecking jQuery? Check.
No semicolons? Check.
Put together by Paul Irish? Check.
Yep, this submission is Hipster 1.0 Certified.
- VeejayRampay 11y ago1) Paul Irish is a former jQuery team member and he has blog entries about how much he's learned from reading jQuery's source code, not sure exactly how he's "wrecking jQuery". 2) Semicolons are still in debate amongst the JS community, I don't think there's a consensus on whether they're needed or not and it seems to boil down to personal preferences. 3) This reeks of ad hominem, how exactly is Paul Irish "hipster"? Strikes me as someone who's done a lot of work to make the web a better place for all of us, fine with me.
- jbrooksuk 11y agoFWIW John Resig himself originally wrote jQuery without semi-colons but it caused all sorts of issues very quickly!
- riquito 11y ago> 2) Semicolons are still in debate amongst the JS community, I don't think there's a consensus on whether they're needed or not and it seems to boil down to personal preferences. They are needed. This var func = function(x) { console.log(x); }; func; (42); is different from this var func = function(x) { console.log(x); } func (42)
- bakabaka9 11y agoThey aren't needed at all. Semicolons in JS are optional. Knowledge about how ASI works, on the other hand, is mandatory.
- mootothemax 11y agoThey aren't needed at all. Semicolons in JS are optional. Which becomes really annoying if you just want to quickly concatenate a few files together. Sure, it's something that you can fix yourself; I'd consider it a common courtesy to include at least one, though.
- bakabaka9 11y agoIn the worst case, you can do something along the lines of [file1, file2, file3].join(';\n') albeit if you're using any kind of post-processing tool, like UglifyJS or Google's Closure compiler, then everything just works, as these tools actually parse JavaScript. There's usually no need to explicitly concatenate the code prior to minification or other processing.
- BinaryIdiot 11y ago> They aren't needed at all. Semicolons in JS are optional. So they're not really optional. What they are is necessary and if omitted the engine attempts to put them in automatically. The rules for automatic semicolon insertion are in section 7.9 of the ECMAScript 5 standard (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf http://www.ecma-international.org/publications/files/ECMA-ST...). This means you're omitting something that is required by the engine but not necessarily required in the syntax which boils down to you losing control of where semicolons end up possibly making your code do something you did not intend because you either misunderstood the rules of automatic semicolon insertion or there was a bug in the syntax parser being used.
- bakabaka9 11y agoYou're only "losing control" if you don't know how ASI works, which is well-defined in the ECMAScript standard. Bad programmers that can't understand ASI will be also confused by the floating point semantics, promises and other basic concepts — there are many ways of "making your code do something you did not intend" in JS, but that's no excuse for bashing language features. Fact check: ASI is a fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board), so demonizing it is every bit as silly as e.g. deprecating C macros. Learning the language helps with the the issues you outlined, while magical thinking (uguuu, semicolons good, no semicolons bad, uguuu) for the most part doesn't.
- BinaryIdiot 11y ago> Bad programmers that can't understand ASI will be also confused by the floating point semantics, promises and other basic concepts — there are many ways of "making your code do something you did not intend" in JS, but that's no excuse for bashing language features. This is a terrible attitude. There is no reason you may not understand every case in which a semi colon is automatically inserted but can still understand floating point, promises and other things. Automatic semicolon insertion isn't really a "basic concept"; it's a convenience for those who elide them and nothing more (the ECMAScript 5 standard even states this). > Fact check: ASI is a fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board), so demonizing it is every bit as silly as e.g. deprecating C macros. I'm not "demonizing" it but if you look at the standard the engine still requires semicolons it just fills them in for you if you miss them. This is vastly different than your deprecation of C macros example. Yes it's documented but it's non-obvious unless you've literally looked it up to learn it. I'm also not sure why you insisted on letting me know it's a "fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board" when I linked to the standard and stated exactly where to look for its definition... > Learning the language helps with the the issues you outlined, while magical thinking (uguuu, semicolons good, no semicolons bad, uguuu) for the most part doesn't. Sigh. Automatic semicolon insertion is only documented. There are no errors or warnings or other information. Many developers, who understand more advanced topics, may not even know about it. The language does nothing to help with this. It's far better to be more explicit than assuming everyone who looks at your code understands the full ECMAScript 5 standard.
- wldlyinaccurate 11y agoYou're right that your (very contrived) examples are different. But if anybody on my team writes code like this, it doesn't pass code review.
- bshimmin 11y agoSure, those examples are quite contrived. Here's one that's not: console.log("woof") (function() { console.log("foo") })() Without semi-colons, that blows up (with a super unhelpful error message, to boot). Sure, if you understand ASI in JS, then you'll figure out the problem very quickly. But if you just always use semi-colons, then ASI is one fewer thing to have to worry about.
- insin 11y agoAlternatively: console.log("woof") void function() { console.log("foo") }() There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement. On this topic, it could so easily have been different; I found this interesting from Brendan Eich [1]: > I wish I had made newlines more significant in JS back in those ten days in May, 1995. Then instead of ASI, we would be cursing the need to use infix operators at the ends of continued lines, or perhaps or brute-force parentheses, to force continuation onto a successive line. But that ship sailed almost 17 years ago. > ...My two cents: be careful not to use ASI as if it gave JS significant newlines... [0] this is technically a lie because everybody needs learn the other one whether they use semicolons or not: don't put a linebreak after the "return" keyword :) [1] https://brendaneich.com/2012/04/the-infernal-semicolon/ https://brendaneich.com/2012/04/the-infernal-semicolon/
- atirip 11y agoA very simple rule - if line starts with '[' or '(', put semicolon before. Thats it. Personally, if all is encapsulated inside self executing function, i put one semicolon before and one after it.
- rtp 11y agoWhat about this? function v(x) { return x; } v("yeah"); returns undefined
- atirip 11y agoWhat about it? Yes, it returns undefined. Has nothing to do with semicolons. You can add semicolon to every line of your example, or remove each and every one and still it returns undefined.
- CatsoCatsoCatso 11y agoHe's putting it in comparison to the previous example https://news.ycombinator.com/item?id=9718410 https://news.ycombinator.com/item?id=9718410 Newline matters / doesn't matter, the same with the semicolons.
- notNow 11y agoActually semicolons are necessary. For example, try writing a rather simple script wrapped and stored in a Chrome bookmarklet without inserting semicolons and see your 100% working script crumbles under the weight of all those unexpected exceptions.
- nailer 11y ago>> Tries to play it cool by wrecking jQuery? > not sure exactly how he's "wrecking jQuery" The grandparent is misguided, but I believe they mean using '$' as the QSA. The grandparent probably uses script tags and globals in which case there would be a conflict if a script tag library requires window.$. JS modules should var $ = require('jquery') (or equivalent) and then use the $ in their local scope if they need jquery.
- bad_user 11y agoWhat modules? You mean CommonJS or was it AMD or ECMAScript 6? Still a straw man IMHO :-)
- nailer 11y agoCommonJS, since npm (which is mainly commonjs) is a few hundred/thousand times larger than the whatever else.
- tracker1 11y agoIIRC, there is a jQuery global object available.. you can assign it to a local variable inside in iife, which is the appropriate way to use jQuery when writing a module... (function(window,$){ //module code goes here }(this.self || this, jQuery)); In this way, you are keeping jQuery itself and your implicit scope assigned to localized variables. If you are writing a modern module (not necessarily jQuery based), then following node/cjs conventions is probably best. If you use this convention with such a plugin, said module should probably return the jQuery object that has been modified, and extra care should be taken that all plugins are using compatible/same instance of jQuery.
- omegote 11y agoHow is he hipster? Oh, he is. This very snippet shows how regrettably hipster the web development scene has become. It's all trend-driven. And particularly Paul, as popular as he is, should be real carefuly about his opinions, because he's a trend-setter. The moment he says jQuery is useful (like here https://docs.google.com/document/d/1LPaPA30bLUB_publLIMF0RlhdnPx_ePXm7oW02iiT6o/edit# https://docs.google.com/document/d/1LPaPA30bLUB_publLIMF0Rlh...) many developers will think likewise. The moment he spits this utter crap despising jQuery, many developers will, again, adopt the same opinion without hesitation.
- deleted 11y ago[deleted]
- paulirish 11y agoI'm using standard [1] for style. I love semicolons too, but am giving it a go without them here. TBH, it feels kinda weird, and I'll probably end up on semi-standard [2] in the end. [1] https://github.com/feross/standard https://github.com/feross/standard [2] https://github.com/Flet/semistandard https://github.com/Flet/semistandard
- dang 11y agoThis is the kind of shallow dismissal that we need less of on HN. Please don't post like this. I'm sure there's a thoughtful way to make your point, and that would be welcome. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html