2

I've been thinking a lot about the way that jQuery is implemented in Javascript code. Libraries such as jQuery and Prototype bind to an alias by default; for these examples, they use the dollar sign $. (Underscore, appropriately, uses an underscore character _.)

I understand how this works. I also understand that many libraries provide a noConflict mode.

My question is, why do these libraries use an alias by default?

From what I've seen, aliases like these only seem to let a programmer type less characters when calling a function, which seems trivial. (Honestly - I'm a little biased, because I don't have issues typing long variable names.) I thought that maybe it was for filesize purposes, but with the proliferation of minifiers, it seems like it'd be a moot point (and a form of premature optimization).

On the flip side, the aliases seem to cause a lot of confusion for people using these libraries.

Now, I'm not really arguing against the use of aliases - I'm just wondering why it's the default option for these libraries. Currently, to avoid an alias, we explicitly have to declare that we don't want to use it. Are there specific benefits that I'm missing about the use of aliases for library calls? The only advantage that I could readily think of is if you somehow wrote cross-library code - to swap libraries, you simply swap the alias. I don't think I've ever seen this done, though.

Anyone know the reasoning behind this? I, for one, would really love to know.

Community
  • 1
  • 1
  • 6
    *aliases like these only seem to let a programmer type less characters when calling a function* - What else do you want to know? – Jared Farrish Aug 27 '12 at 01:54
  • When you are using external libraries from a CDN that are already compressed, the default use of the `jQuery` variable cannot be minified because a minifier can only modify symbols that are defined and used within the scope of what it's minifying. It would be possible write code that could let the `jQuery` symbol be minified, but you'd have to assign it to your own alias to do that - so why not build the alias into the library? – jfriend00 Aug 27 '12 at 02:14
  • Further to what Jared said, even if you don't mind the extra typing consider the extra formatting that might be necessary to make the code readable without horizontal scrolling (if a single line of code needs to call `jQuery()` multiple times). Also, regarding the confusion you mentioned I think most is caused by people trying to code in JavaScript without taking any time to actually learn the language. The first two examples you linked to were from people who obviously hadn't learned basic things like what characters are allowed in JS identifiers, and the third isn't about `$` as an alias. – nnnnnn Aug 27 '12 at 02:16
  • theres a jquery default symbol alias plugin that lets you customize this behavior. – goat Aug 27 '12 at 02:30
  • @Jared @nnnnnn I understand your arguments, but I don't get why it's the default. Usually, if a programmer doesn't want to write a lot of code, he or she will explicitly declare an alias for it. So basically, why isn't `jQuery.noConflict()` the default setting? So far, @jfriend has given a logical answer involving minification across CDNs, which was quite informative. Could there be other architectural reasons for this choice? –  Aug 27 '12 at 17:56

2 Answers2

1

"From what I've seen, aliases like these only seem to let a programmer type less characters when calling a function, which seems trivial."

That's what the people who designed VB thought too. "What's this

for (int i=0; i<10;i+=2){
}

Stuff! What's wrong with:

For i as Integer = 0 To 10 Step 2
Next i 

So I save 13 keystrokes! That's trivial."

It's not trivial when this is what we do all. day. long.

Take the jQuery Cycle plugin: http://malsup.github.com/jquery.cycle.all.js. It has 400 instances of $. If you were to use jQuery instead of $, that would be an extra 2000 keystrokes. It's the reason why IDEs now perform autocomplete of parens, etc. etc. etc.

aquinas
  • 23,318
  • 5
  • 58
  • 81
  • I'm one of the "thumb hammer" four-space programmers, I could give a you-know-what for a `tab`, but I will construct a local `$()` passing from scope so I don't have to worry about all that... *crap*. 'Cuz whatever, but I do prefer the shortform. – Jared Farrish Aug 27 '12 at 02:06
  • I do understand that it's not trivial, but I still don't understand why libraries choose to use the alias by default. Keystroke saving is (rightly) done by the IDE. Why is there a `jQuery.noConflict()` instead of a, say, `jQuery.useAlias()` or something? I just figured that there was a larger, architectural reason for this. Someone mentioned minifying from a CDN - that's the best reason that I've seen so far to make aliases the default choice. –  Aug 27 '12 at 17:50
0

Using jQuery and Prototype together is not good idea. Ideally there must be only 1 javascript framework for the site, and a lot of calls to it. So the best (default) choice is to use shorthand alias for it's main function.

Anyway, if you need to use several libraries/frameworks together on the same page, and you want to avoid conflicts, you can use noConflict mode.

Ruben
  • 31
  • 4