r/javascript • u/raynos • Nov 03 '11
jQuery vs Plain old javascript
http://raynos.org/blog/14/jQuery-vs-POJS11
9
u/alexdavey Nov 03 '11
4
Nov 03 '11
We know where it came from :P thats why its funny!!
3
u/alexdavey Nov 03 '11
Fair enough :) but it sounds like some people are taking it seriously... ;)
1
5
u/djsdotcom Nov 03 '11
Lots of rage. jQuery is good for lots of stuff. Some times you shouldn't (or don't need to) use it so... don't. End of story.
1
Nov 03 '11
Out of curiosity when would you say you needed to use it?
2
Nov 04 '11
I've got a great example: I recently had to develop a website for a 'major' film release. It was originally going to be developed in flash, due to the ability to maintain a strong, 'slick' aesthetic - fading elements, graphics being animated across the page, roll-over graphics on everything. I showed that I could do something equally as good with HTML and jQuery, and I ended up building the entire thing with cross-browser HTML. Trying to get the site working across multiple browsers and devices with the same amount of DOM animation etc without jQuery would have been almost impossible.
1
0
u/k3n Nov 03 '11
When you have tight deadlines and don't have the time to build requirements/spec out a full DOM cross-browser compatibility library that encompasses browsers that you can't even reliably test on your hardware.
So, instead of taking blind stabs in the dark and copy/pasting code from some geocities-like website, and dealing with the hundreds of hours of testing and refactoring required to support IE6 or IE on Mac or whatever, you go to jQuery.com, download, and get to working on the actual project within 30 seconds.
Or, you could waste 100's of hours writing your own library due to your NIH-xenophobia and not even have your lib finished by the time the due date hits.
2
u/raynos Nov 03 '11
We are not objecting to you using a tool for cross browser compliance. Writing your own is time consuming.
We are objecting to you using jQuery as your cross browser compliance tool when better options exist.
1
1
Nov 03 '11
To be honest its really only blind stabs in the dark if you don't take the time to learn your trade. By your logic we should all be using GUI site builders then to really get stuff done.
The other mistake you make is assuming you need a full fledged library to get things done.
4
u/k3n Nov 03 '11 edited Nov 03 '11
To be honest its really only blind stabs in the dark if you don't take the time to learn your trade.
So, according to you, I should have memorized every single nuance and bug for every browser that is available on every platform? Or are you expecting me to procure every platform and browser combination? Either way, that's impractical, and you know it.
By your logic we should all be using GUI site builders then to really get stuff done.
What kind of strawman argument is that? Care to actually offer a point of contention, or will you be content with your sweeping generalizations?
And besides, GUI page builders work great for some people. My cousin needed a website for a garage sale, and she whipped one up in a GUI in under 15 minutes. Does it look "web 2.0"? Not really. Is it a shining example of best practices and UX mastery? Not at all. Did it fulfill her needs perfectly? Yup.
They're also used all the time for silly eBay storefronts and whatnot, but does it matter? It's solving their problem, so wtf do you care how it was solved?
The other mistake you make is assuming you need a full fledged library to get things done.
Congrats on hopping on the microframework bandwagon. I'll see you in a year when you realize that loading up 19 different "micros" with incompatible API's, differing support points, overlapping features, and varying quality just isn't as good in practice as it is in your brain.
2
u/raynos Nov 03 '11
The term "microframework" is poorly coined. All it means is unix philosophy. Use a tool to do one thing and one thing well. Use many of them.
And yes they do work, if you pick the right ones.
If you want to you can go back to your monolothic frameworks to do everything for you, including holding your hand.
2
u/k3n Nov 03 '11
Go back? That'd require me to first leave, which I have not done.
And don't get me wrong, I'm far from a JQ fanboy, but at the same time, it has a much better (proven) track record than
MomAndPop.js, and if that means that I don't have to reinvent the wheel and/or spend untold amounts of time researching and testing micros, then that means more time for me to work on my core business functions instead of wasting time on problems that other people have already solved, likely better than I would be able to solve them in the first place.Are you sure that you're not David Mark? If not, you share the same penchant for dissing jQuery while at the same time pushing your own library, which does give the impression that you're motivated by reasons other than the relative quality of code between the 2 libraries.
2
u/raynos Nov 04 '11
Sorry, I don't like jQuery. And I've made the effort to solve the problem jQuery solves in a better manner using my own library.
So it makes sense to me that my library is a better alternative. It's not about self promoting, it's about showing people what I fill the jQuery gap with.
2
1
Nov 03 '11
GAAH you said Web 2.0... oh my lord...
Your arguments honestly just scream "I dont feel like learning I just want to follow what this framework does blindly!" And yeah that works for some people, but those are the people we are complaining about.
As for the micro framework argument.. they are micro.. so small in fact you can actually read and understand everything going on (again goes back to learning your trade). In reality its much easier to find an incompatibility or an issue if one exists.
MS Word solved peoples problems for web development, and just caused problems for the people who actually had to come in fix and maintain the mess....
The whole point of the argument is this, tools that make it easy for the lamon, or people who don't care also makes it easy to make absolute garbage and cause a lot of headaches. If used properly I don't have an issue with jQuery I've written a couple of plugins myself, and have the jQuery bronze badge on SO. However I don't think theres ever an argument where you HAVE to use jQuery over JavaScript, other than for convenience to the developer.
1
u/k3n Nov 03 '11
GAAH you said Web 2.0... oh my lord...
Yes, because I lived and coded through that time, and I'm seeing many similarities with the current microframeworks and NodeJS trends.
As for the micro framework argument.. they are micro.. so small in fact you can actually read and understand everything going on (again goes back to learning your trade). In reality its much easier to find an incompatibility or an issue if one exists.
The whole point of using someone else's code is that *I* don't HAVE to read or understand it; as long as it works (and doesn't have severe issues with performance, stability, etc.), then that's all that I really care about! It has nothing to do with "learning your trade", it has to do with not wasting time on things that you don't have to.
And NOBODY used the phrase "have to", that's all you. The OP used the word "need".
1
Nov 04 '11
Hey hey, I worked through the whole web 2.0 era too :?. While I agree to an extent with a portion of what you are saying (I like to play devils advocate) I see nothing wrong with NodeJS.
5
u/mrPitPat Nov 03 '11
anyone else click on "delete post" button? Just to see what would happen?
1
2
Nov 04 '11
Seriously though, trolls aside, jQuery is a horrible library.
I agree, to an extent. The API is ridiculous. Today I ran into $(selector).before() expecting the $(selector) to be placed before whatever was in the before function. Wrong. There are tons of little things like this that make me question a person's choice in jQuery and no other lib.
3
0
u/Wolfy87 Nov 03 '11
@raynos I agree completely. Would love to know your thoughts on MooTools though. I use it in pretty much every project but only when required.
If some site only has one event to show an element I will polyfill the events myself (attatchEvent -> addEventListener, I hate you Microsoft) and save the page weight 20kb or so.
I havent moved into using micro libraries yet. Although I have loved coding my EventEmitter. I can see the benefits of using these small specific libraries over huge all purpose ones.
It's quite nice to add your own $ shortcut too.
(function($) {
var myElement = $('myElement');
}(document.getElementById));1
u/raynos Nov 03 '11
I heard you like promoting broken code.
var $ = document.getElementById
$("foo"); // throws errorMooTools is a better library then jQuery, although personally I just use the DOM-shim.
1
u/Wolfy87 Nov 03 '11
Derp. Sorry about that. You actually have to make the $ shortcut run
document.getElementById. Like this.function $(id) { return document.getElementById(id); }The DOM shim looks great. Well done! It's nice to have the classes that MooTools provides. Although native ones will be a lot better they do the job.
I am sure there is a micro framework for classes anyway.
2
2
u/SkeeballAvenger Nov 03 '11 edited Nov 03 '11
Hi, I'm pretty new to JavaScript, and I'm trying to understand this syntax. I've seen it multiple times, but I don't quite understand why it's written this way.
(function(xyz) { // do something here }(abc))Could someone help me understand the purpose of this structure, and what it's doing? I'd appreciate it.
3
u/Wolfy87 Nov 03 '11
Sure. This method of encapsulating your script essentially sandboxes your code. All code within the wrapper will be run but any variables / functions will not leak out into the global variable namespace unless you specifically tell them to.
Heres a little example.
(function(globalObject) { function foo() { // This function is private to this wrapper // It can only be called from inside this wrapper } // This line will drop our foo function into the global object // This works because we are passing `this` to our anonymous function // As long as it is not nested then `this` will equal `window` or `exports` depending on your JavaScript environment globalObject.foo = foo; }(this));It can be a little confusing. But this helps you to create more modular code that will not pollute the browser with random variables and functions.
Just ask if you still don't get something :)
2
u/SkeeballAvenger Nov 03 '11 edited Nov 03 '11
Thanks for the assistance! Okay, I think I understand, let's see if I have this right.
The (function(globalObject)...) syntax is an anonymous function which is being called (in this example) with "this" passed as an argument, and assuming a typical browser context, would be the window object. In a node.js environment it would be the 'exports' global object.
Then, the global object is being extended with the up-until-now- private function "foo", so that we can call foo() outside of the wrapper. This looks to me like a closure, but I'm not sure that it's entirely accurate to call it that.
I do, however, understand this syntax much better, thank you! I'm saving this comment away to review again a few times to make sure I've really got it.
I'd like to ask, I've seen "namespacing" done by assigning an object to a variable like so:
var myThing = { foo: function () { // Do things here }, bar: new Array; };Is there a reason to use the former over the latter, or is it more a matter of preference?
Thanks so much, I feel like I'm overcoming a big knowledge hurdle here. I really should find a good reference for learning to code in this style, because it feels much more modern to me than what I've been learning.
2
u/Wolfy87 Nov 03 '11
You hit the nail on the head! Thats exactly how it works.
I have never used name spacing myself in my JavaScript but I suppose the major libraries are prime examples. jQuery sticks everything in the
jQueryglobal and creates an alias of$. MooTools splits the whole library up into multiple global objects such asFxandRequestand performs a lot of extension of existing globals.Elementfor example.I think if you are trying to write modular code that can just be bolted together then name spacing is a great idea, it just helps to stop a conflict. I think it also helps as a kind of source code annotation. You can see that
ui.buttons.Submitwill be a button that will submit something and is part of the user interface.I have experimented with name spacing in a script I wrote called Package.js. But that is all about checking if a script has been loaded via a namespace and loading it if not. Like Java's import. Well, I say like it, it is no where near as good.
So they are both good, they both keep code separate and clean. I would suggest using name spacing for larger projects and more reusable code. But I would use name spacing in conjunction with the sandboxing wrapper, just so you can define private functions etc. It also allows things like Google Closure Compiler to minify your code better.
2
u/SkeeballAvenger Nov 04 '11
Awesome,, thanks so very much for taking the time to spell this out for me, it's been more help than you know... For the first time in days I feel like I'm actually improving the quality of my code.
The distinction between name spacing and (whatever the proper name is for the techinique you've explained to me above is) interests me, I think I need to go play a bit and drop some code through a profiler to see if it makes any difference as well. I can certainly see the benefit of creating a new object though like the "big guys" do, too.
I will admit to being a jQuery guy, although it's more for convenience than anything (selectors). I suppose I just need to get off my duff and learn more about mootools and perhaps roll a few of my own complex selectors, to get over it.
Thanks again Wolfy!
1
u/jmking JSX is just PHP in the browser Nov 03 '11
+1 for Mootools.
I realize the OP is tongue and cheek, but I still don't like jQuery. I find it's messy, the effects libraries are weak, and relies too heavily on 3rd party plug-ins with their own version and 3rd party plug-in dependencies.
I also like how you can take as much or as little of the library as you need per project.
1
Nov 04 '11 edited Nov 04 '11
It boggles my mind how people can be so vehemently opposed to jQuery. I've been using it for years and find it to be a fantastic library.
Of course it has its faults; it would be foolish to assume that a library as enormous and ubiquitous as jQuery WOULDN'T have any. And yes, I'm sure writing your own library would probably be a preferred solution for those with the time and inclination.
But the fact of the matter is this: not a single microframework or set of micro-libraries out there is even as REMOTELY well-documented or widely-used as jQuery is. What does this mean? It means no one is going to use your fucking alternative, because no one fucking understands it nor has the time to understand it.
I'm all about building better alternatives. More power to you if you're the type of person creating these solutions. But you have to realize that for those of us who spend our time creating and shipping actual applications, we have to prioritize, and writing our own library or learning a new one to save 20k on bandwidth and obtain more JS street cred isn't high on the list.
*edit - Oh, and to your bullet point:
"infinite amounts of pain when they don't work (and anybody who tells me that selectors and especially effects are stable and cross platform is just so full of BS that it's not even funny)" What? I've produced dozens and dozens of sites/applications using any number of cryptic, mind-bending combinations of selectors and/or effects and never had a single issue (that I can recall) with any of them NOT being cross-platform... Can you provide some sort of example?
In my eyes, if the resultant jQuery you're producing doesn't immediately strike a visual parallel to vanilla JS, you're fucking doing it wrong. The entire fucking backbone of jQuery itself is to be used for its awesome cross-browser, cross-language compatibility. Everything else is just icing on the cake.
1
0
u/MetaMetaMan Nov 03 '11
I've never been very keen on jQuery, but I do use Dojo quite a lot. I prefer to use POJS when convenient, but for things like drag-and-drop functionality and templates, I find that Dojo is really useful.
I guess I would like to get your input on Dojo. I rarely see anyone post anything about it.
7
u/joshrice Nov 03 '11
(Not sure if serious or....but I'll bite.)
It seems the crux of your argument is because jQuery lets you do 'horrible' things, it must be 'horrible', which is horribly faulty logic. I can use a car to murder someone, but that doesn't make the car bad. With great power comes great responsibility and all that...
I'd like to see your cross browser implementations of fadeIn/Out, animate, ajax, etc... or do you prefer a different library? Or do you just not do anything like that?
One of the nice things about jQuery or moo tools, or whatever, is not having to reinvent the wheel. Sure I could write some POJS to do anything jQuery does...but why? My clients and I have much better things to do with our time.