Live data from Hacker News

Bling – the $ of jQuery without the jQuery

gist.github.com

71–80 of 138 posts

Re: Bling – the $ of jQuery without the jQuery

#71

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…

wow, storm in a tea cup much? He clearly mentions implementing '$', and nothing else ...

Re: Bling – the $ of jQuery without the jQuery

#72
post #68
post #60

Earlier quoted context omitted.

A 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.

What about this? function v(x) { return x; } v("yeah"); returns undefined

What 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.

Re: Bling – the $ of jQuery without the jQuery

#73
post #37
post #2

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…

wrap elements in a node list This is not correct. You may say that it wraps or outputs elements in a node list like object but it is not the real deal. If you examine the emitted jQuery objects using instanceof or any other method, you'd see that they are not related to NodeList in any way. They're just generic objects of Object type.

True, jQuery doesn't use the native NodeList for various reasons, but since QSA returns a NodeList, that'd be the equivalent for this "substitute".

Re: Bling – the $ of jQuery without the jQuery

#74
post #71

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…

wow, storm in a tea cup much? He clearly mentions implementing '$', and nothing else ...

`$` is the point of entry to almost everything jquery provides, so he's not implementing jquery's `$` since the gist does not come even remotely close to jquery's featureset. He's implementing a completely different function with the same name.

If behaviour is considered irrelevant, here's my one-line implementation of $:

    var $;

Re: Bling – the $ of jQuery without the jQuery

#75
post #73
post #37

Earlier quoted context omitted.

wrap elements in a node list This is not correct. You may say that it wraps or outputs elements in a node list like object but it is not the real deal. If you examine the emitted jQuery objects using instanceof or any other method, you'd see that they are not related to NodeList in any way. They're just generic objects of Object type.

True, jQuery doesn't use the native NodeList for various reasons, but since QSA returns a NodeList, that'd be the equivalent for this "substitute".

> jQuery doesn't use the native NodeList for various reasons

Those reasons being:

* because it's garbage

* because native objects shouldn't (and may not necessarily be) extended

* because static nodelists didn't even exist back when jquery was created

* because you can't create a nodelist in userland code

Re: Bling – the $ of jQuery without the jQuery

#76

Earlier quoted context omitted.

> "document.querySelectorAll(selector)" is a bit verbose Right. 35 characters. > "…but jQuery API is no better" Your jQuery example is 11 characters. I can’t believe you’re using verbosity as a measurement, while at the same time claiming 35 and 11 are the same. Does. Not. Compute.

Sorry for confusion, what i meant was: yes DOM API is a bit verbose, but jQuery is exactly the opposite in a bad sense: it's using too few words to express too many different things.

jQuery uses CSS selectors, which is fairly clear if you have a general understanding of them.

Re: Bling – the $ of jQuery without the jQuery

#77
post #72
post #68

Earlier quoted context omitted.

What about this? function v(x) { return x; } v("yeah"); returns undefined

What 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.

He's putting it in comparison to the previous example https://news.ycombinator.com/item?id=9718410

Newline matters / doesn't matter, the same with the semicolons.

Re: Bling – the $ of jQuery without the jQuery

#78
post #9
post #4

Earlier quoted context omitted.

I prefer:- function $(q, parent) { return (parent || document).querySelector(q); }

I'd also wrap it in a `[].slice.call`: function $(q, parent) { parent = parent || document; return [].slice.call(parent.querySelectorAll(q)); }

Also see ES2015's Array.from()

    Array.from()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re: Bling – the $ of jQuery without the jQuery

#79
post #67
post #19

Earlier quoted context omitted.

(var | let | const) $ = ::document.querySelector; with experimental ES7 syntax :D https://github.com/zenparsing/es-function-bind

Huh, my joke actually turned into me learning something, thanks!

Hehe, You're welcome – I do take golfing very serious ;)
Post reply on HN