Live data from Hacker News

Vanilla JS, fast, lightweight, cross-platform framework

vanilla-js.com

121–130 of 134 posts

Re: Vanilla JS, fast, lightweight, cross-platform framework

#122
$.ajax({ type: 'POST', url: "path/to/api", data: "banana=yellow", success: function (data) { alert("Success: " + data); }, });

can be written as

jQuery.post('path/to/api',{banana:yellow},function(data){alert("Success: "+data);});

much simpler and easy than

var r = new XMLHttpRequest(); r.open("POST", "path/to/api", true); r.onreadystatechange = function () { if (r.readyState != 4 || r.status != 200) return; alert("Success: " + r.responseText); }; r.send("banana=yellow");

nevermind got the joke. but i think jQuery helps write faster code sometimes

Re: Vanilla JS, fast, lightweight, cross-platform framework

#123
post #89
post #52

Earlier quoted context omitted.

The jquery one will be doing a lot of extra useless inefficient grunt work Such as?

"Useless" is subjective, but the jQuery object does a lot of extra work above the vanilla example. $ isn't just a syntax layer on top of querySelectorAll. When you do $('p'), it first queries the DOM for everything that matches that selector, then it creates new jQuery objects to wrap each of the returned nodes as well as creating a jQuery object to contain them. If all you're doing is setting the innerHTML of these…

It creates just one jQuery object to wrap the entire array of DOM nodes. Afterwards, .html() is essentially innerHTML [1], provided jQuery can use it directly (otherwise it has to create DOM nodes by itself).

[1] https://github.com/jquery/jquery/blob/master/src/manipulatio...

Re: Vanilla JS, fast, lightweight, cross-platform framework

#125

Earlier quoted context omitted.

I am a c developer, now I want to learn jQuery , but its syntax seems so weird to me, Vanilla JS looks more comfortable to me , one questions, does it supported by webkit?

I'm sorry. I don't know if you're joking or are serious. But if you're a C programmer and have no experience with JS I guess it must be really easy to be fooled by this "joke". Some JS people (the ones usually with a long beard) hate how kids use frameworks like jQuery and this site is an attempt at telling them "The vanilla JS, i.e. the standard language that all browsers use and your cool jQuery leverage under the…

I want to learn something about jQuery,because recently I need to develop an application which use webkit. It has a feature that need to highlight some special words. After search in the internet, I found the jQuery can 'easily' do that. But the syntax of jQuery make me headache, then I happened to see this article. Thank you very much , I am very appreciate for your advice:)

Re: Vanilla JS, fast, lightweight, cross-platform framework

#126
post #75

Given how optimized jquery is, does anyone understand why something as simple as getting an element by id would be so much slower than Dom methods?

I'm surprised by the number of comments like this. jQuery has to parse the selector and figure out that it's of the form "#id". This requires running a regular expression. A lot more is happening. The whole jQuery call from start to finish takes 2.85 microseconds, in what is presumably a real benchmark, but microbenchmarks like this are hard to interpret and basically meaningless. But yes, if your app needs to do a b…

> if your app needs to do a burst of 350,000 jQuery calls in a tight loop and you are bummed that the whole thing takes a full second, you should then optimize using document.getElementById

Seems like you might have some other optimization work to do at that point :)

Re: Vanilla JS, fast, lightweight, cross-platform framework

#127
post #112

Earlier quoted context omitted.

That's ES3. You have no reason to choose that way anymore - ES5 loops work everywhere current, can be polyfilled into IE8, and don't require any boilerplate stuff. var paragraphs = document.getElementsByTagName('p'); paragraphs.forEach(function(paragraph){paragraph.innerHTML = 'Hello.';})

Except #forEach is a method of Array, and getElementsByTagName does not return an Array but a NodeList. Which doesn't have any of Array's methods (it has a length, it can be indexed, and it has an alternative indexation method - #item — but it's not an array at all)

You're right. I wasn't sure earlier, as I was typing on a phone with no JS console.

but never mind:

Array.forEach.call(paragraphs, function(paragraph){paragraph.innerHTML = 'Hello.';})

will work fine.

Re: Vanilla JS, fast, lightweight, cross-platform framework

#128
post #29

Earlier quoted context omitted.

This enumeration is done the wrong way. You enumerate over a DOM node list which looks like an array, which includes enumerable properties like "length". This means that the variable "p" at some point will be "length" and "length".innerHTML = "..." will produce an error. The proper way is: for( var i=0, ps=document.getElementsByTagName('p'), len=ps.length; i < len; i++) {ps[i].innerHTML = 'Hello.';}

I'm a JS noob, but I though 'length' was not enumerable (i.e. its 'enumerable' property is set to 'false'). Edit: I think I missed what you said. You're saying it's "like" and array, but unlike arrays its `length` is enumerable. I'll leave my comment so other skeptics can benefit :) Edit2: This is what I was referring to (read the 'Note' at the right side): http://bonsaiden.github.com/JavaScript-Garden/#object.forinl…

Looks like this is correct:

paras = document.getElementsByTagName('p') for (var para in paras) { console.log(para)} 0 1 ... 9 10 length item

Re: Vanilla JS, fast, lightweight, cross-platform framework

#130
post #25

I can't be the only one getting tired of sites like this making the front page. Really? It would be one thing to see someone write a legitimate article on why they think the move to JS frameworks is harmful and/or the benefits of using plain JS, and for that to make it to the front page. I'd be interested in that perspective. But this is just somebody being snide. It's the internet equivalent of the kid on The Simpso…

Reading the example code I learned enough to become interested in vanilla javascript. Although satirical in tone, I found it insightful. It lead me to question the necessity of using jQuery for every project.

I used to wonder the same thing, but after a while I've found that it already pays to include jQuery (or similar) as soon as your code reaches a complexity of "does more than one kind of thing" (roughly).
Post reply on HN