Skip to main content

jQuery: When does a library become a language?

jQuery is a great JavaScript library which allows web developers to write cross platform code, but some of its syntax looks like it is based more on Ruby than JavaScript. When does a library become a language?

So first of all, what is the difference betwen a programming library and a programming language?

A programming language is a means by which we (as humans) can give instructions to a computer to get it to do various tasks (algorithms, computations, etc...). They are largely artificial constructs created for human readability which then need to be compiled or interpreted into actual instructions which the computer can then carry out. A compiler turns a program into machine readable instructions ahead of time (compile time) where as an interpreter does so on the fly (runtime). Languages such as C and Java are generally compiled and JavaScript, Perl and Ruby are generally interpreted. While there is no real reason why this has to be the case, these factors normally affect the syntax of the languages in question.

A library is a collection of functions and methods of (normally) common actions which are not expressed in the original language. These are not included in the main language because not everyone will need to use them all the time. They are also written independently of the main language compiler/interpreter and they also encourage code re-use (so not everyone needs to write their own String.trim() function for example).

Because of the fact that there are many JavaScript interpreters out in the internet and JavaScript developers cannot control which one will be used to interpret their code, a few years ago some people released their own framework/libraries which iron out these differences and allow developers to write one set of unified code from which any browser differences could be abstracted out into the library.

The most popular of these are Prototype and jQuery.

jQuery primarily gives developers a generic way to access and manipulate DOM elements (one of the main differences between browsers).

However, the syntax used looks very different from plain old JavaScript.

Here is a typical JavaScript loop.

var elements = getElementsByTagName("div");
for (var i = 0; i < elements.length; i++){
alert(elements[i].id);
}

Here is the same loop in jQuery

var elements = $("div");
elements.each(function(i){
alert($(this).attr("id"));
});

Do these look remotely alike?

How about this?
JavaScript
var element = document.getElementById("myDiv");
alert(element.innerHTML);

jQuery
var element = $("#myDiv");
alert(element.html());

To the untrained eye, one might think these are different languages.

jQuery is not another language though, not only does it use the same JavaScript interpreter, not only is it useless without JavaScript (i.e. it is not independent), but in jQuery you are really creating and manipulating a jQuery object and you are given the option to access the JavaScript object it references using the get() method.

It is possible though to write a language based on another language. The Ruby runtime is essentially built on the C++ framework. JavaScript runs in browsers written in C++ as well. These languages have no direct relation to C++ in their syntax though. In theory you could probably write a C++ compiler in Ruby should you want to (though that would be pointless and slow). You can write programs which translate programs from one language to another (the Google Web Toolkit translates Java into JavaScript).

I would argue that jQuery is a dialect of JavaScript or else even a kind of JavaScript slang. It differs from the main language, but it doesn't veer far enough to be completely independent or incomprehensible.

Comments

Joao said…
Hello there!

Ruby is actually based on C.

JavaScript is more usually based on C++ and I really like what the Google guys have created in their v8 JavaScript engine.

jQuery is impressive for the author really kept focused on reusing the approach for all of its tasks, but when I tried to read its code I found it quite tough, just as tough as when trying to read people's jQuery plugins, each saving data differently and trying too much to keep the conciseness. ;-)

Prototype and all other libraries work quite differently to jQuery and it seems as if the development of Prototype has stalled as of late even though it also means that it does not introduce changes that could break people's code. jQuery's popularity is very impressive and deserved!

Tools such as jQuery can be much more easily built using languages that allow more creative liberty. jQuery's success is a clue to why people seek much higher level languages than they usually get when using C, C++, Java and so on. And just as jQuery doesn't cease functioning despite all kinds of developments, just so do the tools that are built in these extremely higher level languages like Ruby and so on. Just as jQuery helps with really exercising the browsers' capabilities, tools built for instance in Ruby also really help with fixing bugs and improving the quality of the underlying platform.

There is a lot of work to make such a magic work, to make languages a success, and the roles of the libraries is to facilitate some kind of programming and not to become entire languages themselves. ;-) In Ruby, mini-languages called Domain Specific Languages of which jQuery is an example are very popular. DSLs in Ruby really push the envelope and might even scare guys like the creator of Ruby, matz, who might not like everything that he sees when users allow their creativity to flourish.

Cheers.
Anonymous said…
It might be interesting to notice that the original Javascript inplementation was written in Lisp.

You might then consider exploring a bit more the "lambda calculus side" of Javascript and find that, in fact, jQuery functional style isn't that "different" at all.
Douglas Webb said…
For what it's worth, on the "compiled vs interpreted" comparison, Perl and Java are pretty much the same. They both compile their source code to byte code, then run the byte code on a virtual machine. They both inherit this approach from Pascal.

The difference is that Perl's compiler is much faster; so fast that it's generally not worth running it as a separate step. That's why most people don't realize it's there. The speed and compile-at-run-time behavior gives Perl programmers most of the development efficiency of an interpreted language, and also most of the run-time speed and efficiency of a compiled language.
Anonymous said…
Regarding Joao's assertion that "Ruby is actually based on C. JavaScript is more usually based on C++ ..."

I'd say Ruby is strongly influence by Perl's loose syntax and Smalltalk's object system, and JavaScript is actually closer to Scheme than C++.
Anonymous said…
@Anonymous -- When Joao said Ruby was based on C, I suspect the reference was to the Ruby interpreter/VM are built in C, at least Matz's versions (Rubinius being based on C++).
Remember one of the biggest virtues of JavaScript is being "Prototype Based" and that give you a lot of "Beautiful Power". That's impossible or very hard to implement in other languages.

(For getting more familiar with "Prototype Based" programming or enjoying more of it make sure to check out iolanguage.com)
Unknown said…
Well, creating successives layers of languages is a way well known in the functional paradigm to improve the core of a language. The capacity of javascript to handle funtions as high-level "objects" and handle code as data and viceversa let the creation of a new "language" layer (or various) on top of the core language. This approximation is in the 1992 book on lisp of paul graham for example. ops, but where is macros??? :-P

Popular posts from this blog

Master of my domain

Hi All, I just got myself a new domain ( http://www.skuunk.com ). The reason is that Blogspot.com is offering cheap domain via GoDaddy.com and I thought after having this nickname for nigh on 10 years it was time to buy the domain before someone else did (also I read somewhere that using blogspot.com in your domain is the equivalent of an aol.com or hotmail.com email address...shudder...). Of course I forgot that I would have to re-register my blog everywhere (which is taking ages) not to mention set up all my stats stuff again. *sigh*. It's a blogger's life... In any case, don't forget to bookmark the new address and to vote me up on Technorati !

Elixir - destructuring, function overloading and pattern matching

Why am I covering 3 Elixir topics at once? Well, perhaps it is to show you how the three are used together. Individually, any of these 3 are interesting, but combined, they provide you with a means of essentially getting rid of conditionals and spaghetti logic. Consider the following function. def greet_beatle(person) do case person.first_name do "John" -> "Hello John." "Paul" -> "Good day Paul." "George" -> "Georgie boy, how you doing?" "Ringo" -> "What a drummer!" _-> "You are not a Beatle, #{person.first_name}" end end Yeah, it basically works, but there is a big old case statement in there. If you wanted to do something more as well depending on the person, you could easily end up with some spaghetti logic. Let's see how we can simplify this a little. def greet_beatle(%{first_name: first_name}) do case first_name d

Speeding up RSpec

So today I have been looking into getting our enormous battery of tests to run faster. I have yet to find anything that works for Cucumber, but I did find an interesting way to speed up RSpec which is detailed here. https://makandracards.com/makandra/950-speed-up-rspec-by-deferring-garbage-collection Basically, it seems that by not collecting garbage too frequently, you can make your tests run much faster (at the expense of memory management of course). We observed a 30% reduction in the time it takes to run an RSpec test suite. I did try to implement this on Cucumber, however because we need to store much more in memory to set up and tear down our objects, it meant that I kept running out of memory when I wasn't using the default Garbage Collection and the tests took even longer (so, buyer beware). I suppose if you had a small set of features though you might see some benefit.