5 ms·
Sure, jQuery creates a replica of the underlying system (the browser DOM), but it's a better replica in every way. Even if you ignore cross-browser compatibilit
by simonw 16y ago
Sure, jQuery creates a replica of the underlying system (the browser DOM), but it's a better replica in every way. Even if you ignore cross-browser compatibility, compare the following:
$('div').css('color', 'red') v.s. document.getElementsByTagName('div').style.color = 'red'
$(el).replaceWith(otherEl) v.s. el.parentNode.replaceChild(el, otherEl)
$(el).wrapInner(otherEl) v.s. something-too-horrible-to-contemplate
$.ajax('/foo/bar/', function(data) { $(el).html(data); }) v.s (from memory, may be incorrect):
var xhr = new XMLHttpRequest();
xhr.open('GET', '/foo/bar/');
xhr.onreadystatechange = function(ev) {
if (ev.readyState == 4) {
el.innerHTML = xhr.responseText;
}
}
xhr.send(null); // !?
( the jQuery example can be shortened even
further to $(el).load('/foo/bar'); )
jQuery makes the DOM better.
- dstein 16y agoThe inner platform arises out of how the JQuery ($) function/object and all the methods behave. Compare conventional DOM code: var d = document.getElementById('elm'); d.style.color = 'red'; d.onclick = function() {} To JQuery code: $('#elm').color('red').click(function(){}); Now what if you want to add debug statements between the style and event statements? Using JQuery you have to "undo" the inner platform in order to get access to the actual element object: var d = $('#elm').get(0); // debug code $(d).color('red') // debug code $(d).click(function(){}); All of a sudden what made JQuery concise starts getting in the way.
- konad 16y agoThat's where I'm coming from. But I guess it's just years of beind used to doing JS by hand.
- Encosia 16y agoThat seems like an unfairly contrived example. Even so, you should be using a real debugger anyway at this point, not printf debugging. Stepping through tight code like that and observing some watches is very easy in a client-side debugger.
- dstein 16y agoI wasn't talking about debugging techniques. It doesn't matter at all what you do between those statements. Any time you need to access DOM elements or do anything rudimentary JQuery just gets in the way because you can't even make use of local variables and for-loops without resulting in spaghetti code.
- Encosia 16y agoFundamentally, jQuery is just a JavaScript API to normalize the DOM. It doesn't force you to program or organize your code in a certain way (i.e. Spaghetti coding). I'm honestly curious to see a legitimate example of what you mean.
- dstein 16y ago"It doesn't force you to program or organize your code in a certain way" Yes it does. JQuery calls it "new wave JavaScript", but really it's just an anti-pattern. JQuery forces you to select all elements using $() before being able to use any of the methods. And you have to call $().get(0) to get the selected element out. It is this unnecessary process that gets very tiresome. Let's see if this makes it more clear: $('#elm').color('red'); /* do some stuff here */ $('#elm').text('new text'); To simplify that (and not require a redundante DOM call) you'd use a local variable to store a reference to the element: var d = $('#elm').get(0); $(d).color('red'); /* do some stuff here */ $(d).text('new text'); I argue that this is unnecessary when you could just write conventional code instead: var d = document.getElmentById('elm'); d.style.color = 'red'; /* do some stuff here */ d.innerHTML = 'new text'; The way the $() function operates just doesn't sit well with all coders, regardless of how solid JQuery is in other respects.
- Encosia 16y agoThat isn't necessary. This does the same thing: var d = $('#elm'); d.color('red'); // do some stuff here. d.text('new text');
- shib71 16y agoYou could do this by adding your own .debug() function. In some ways this would add an advantage, because you would be making your debug code reusable. Example: jQuery.fn.debug = function(){ if (console && console.log) console.log(this); return this; }; ... $('#elm').debug().color('red').debug().click(function(){}).debug();