This is all I personally ever need. function $(q) { return document.querySelector(q) }
$ = document.querySelector.bind(document);
would be sufficient? ;)11–20 of 138 posts
This is all I personally ever need. function $(q) { return document.querySelector(q) }
$ = document.querySelector.bind(document);
would be sufficient? ;)> [].slice.call( document.querySelectorAll('.foo'), function(){ When do you need to do that?
[].slice.call(document.querySelectorAll(...)).forEach(function () {})
Because a lot of array methods are "generic", i.e. operate on array-like objects instead of just arrays, that can be "compacted" to [].forEach.call(document.querySelectorAll(...), function () {})
but neither are very nice.> [].slice.call( document.querySelectorAll('.foo'), function(){ When do you need to do that?
[].slice.call(document.querySelectorAll('.foo')).forEach(...
It "casts" the NodeList which is "Array-like" to a proper Array, which has forEach, map and other useful methods in its prototype.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.
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.
This does three things: 1. Alias document.querySelectorAll to $. 2. Alias node.on to node.addEventListener. 3. Make NodeList a sub-type of Array (so you can use forEach etc on node lists as you would on arrays). The $ of jQuery also does a few other things, e.g. create HTML elements from text ($(' ') creates a div element with CSS class "foo") and wrap elements in a node list ($(document.body) creates a node list con…
I don't really know what to think about that. On one hand it may be a nice way to show to people that what they use in jQuery can be written in a few lines of pure JS. On the other hand it encourages people who could use pure JS (because they only use a small portion of jQuery) to stick with some non-standard helper syntax.
> to stick with some non-standard helper syntax. Or in other words - replacing an ugly, verbose API with a widely recognised alternate syntax!
jQuery was a game changer for web client side development. Thousands of work hours have been spent on it. It had a specially important role of dealing with all the inconsistencies between browsers. If you don't use all the features, nowadays you can just create a custom build with the stuff you want.
If you don't need jQuery, maybe because you only care about modern browsers, or you use another library or you don't mind a bit of extra boilerplate for DOM manipulation, that's perfectly fine. But these kind of posts seem like mockery on the developers of a library that has provided extreme value for thousands for developers over many years.
This is all I personally ever need. function $(q) { return document.querySelector(q) }
If we're golfing, $ = document.querySelector.bind(document); would be sufficient? ;)
with experimental ES7 syntax :D https://github.com/zenparsing/es-function-bind
Yes, you can re-implement jQuery in 10 lines of code... if you only support 0.1% of the functionality of jQuery. jQuery was a game changer for web client side development. Thousands of work hours have been spent on it. It had a specially important role of dealing with all the inconsistencies between browsers. If you don't use all the features, nowadays you can just create a custom build with the stuff you want. If yo…