5

I'm just developing my own mini framework for an application I was working on, and I've been studying the jQuery is coded.

I get the way $(selector).function() works, but how come you can call some functions such as:

$.ajax()

Surely this would been the dollar symbol references both a function and the jquery.fn object at the same time?

Thanks in advance!

2 Answers 2

8

Functions are objects in JavaScript so they can have properties.

$ is the jQuery object, when using $() it is used as a constructor (it contains some magic so new is not necessary); but it also contains lots of methods (and some non-callable properties such as $.browser) available as $.something

Sign up to request clarification or add additional context in comments.

2 Comments

Could you give me an example of a function that also has a property then. Say that I could call both function() and function.test?
@JamesLeckenby: Simple. var a = function() { console.log("from a"); }; a.test = function() { console.log("from a.test"); }; a(); a.test();
5

http://jsfiddle.net/vZvgv/1/

var $ = function(str) {
    document.write(str+'<br />');
}

$.ajax = function(str) {
    document.write(str+'<br />');
}

$.answer = 42;

$('dollar');
$.ajax('ajax');
document.write($.answer);

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.