3

I have used jQuery for a while and understand the basics of selectors and chains. I am now ready to dive into advanced javascript concepts. Now I'm confused with multi-roles of jQuery.

For example,

jQuery.add(...) 

This seems to me that jQuery is a javascript Object that we can send a message to.

jQuery(function() {...}) or jQuery(...selector...)

This time, jQuery seems to be a javascript Function that can take some parameters.

What is it exactly?

3
  • 1
    Since you can call jQuery, it must be a function. But functions are objects too. You can assign any property to it (like add). Commented Jul 26, 2012 at 9:28
  • 1
    It's a function, but since all functions are objects, it's an object aswell. Commented Jul 26, 2012 at 9:31
  • jQuery is like the matrix; It's everything, and it's nothing. Everything in that it can do pretty much everything you'd ever need, and nothing in that it's nothing more than javascript. Commented Jul 26, 2012 at 9:36

3 Answers 3

2

console.log(typeof jQuery); outputs '> function' :)

Also try this:

var some_name = function() { return 42; };
some_name.some_property = 84;
some_name.some_method = function() { return 63; };

typeof some_name; // outputs "function"

// but... 
some_name.some_method(); // ...is also possible :)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for succinct explanation. This is amazing a function can have a method! This concept can confuse any OOP programmers like me.
@ToyHunter You're not OOP enough ;) JS is object to its core: a function is an object. You're probably coming from a half-assed OOP language :-)
@FlorianMargaine That's not true. It's because Functions are first-class objects in JS in contradiction to other OO-Languages where they are not. This does not necessarily make them less OO.
chester1000, all correct but only of very peripheral relevance to an explanation of jQuery.
0

It is hard to explain more in-depth than how it already describes itself:

jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

Source: jQuery.com

I have always thought of it as another language that utilizes javascript to allow simplicity to coding. It acts as functions to do things in one line of coding rather than 20+, a selector that shortens 50+ characters to 10+ characters, and it does much more.

If you really want to go in-depth to the coding, you can download the development version of jQuery.

jQuery: Development Version

3 Comments

I don't think the question is about what the library jQuery is, but about the value that jQuery (and $) refer to.
@Felix Kling Thanks for clarifying my question.
@FelixKling Ah, that explains it.
0

Firstly, bear in mind that jQuery is written in such a way that :

jquery(...);

is equivalent to :

new jQuery(...);

More detail here

Secondly, $ is a synonym for jQuery.

Thirdly, it is also important to understand that javascript allows functions to be called without assigning whatever is returned. var foo = bar(auguments); and bar(auguments); are both legal statements.

Lastly, javascript functions are first class objects. They can be passed into other functions, returned by other functions, aliased by assignment, and made properties of other objects.

Armed with this understanding, it should be easier to see how and why statements like $(selector); and $(function(){...}); work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.