Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Tuesday, April 17, 2012
How is the "jQuery” var a function and an object?
For example, when you use jQuery('someDiv'); , it's a function, but you can also use jQuery.ajax(...); .
I believe the .ajax example is utilizing the jQuery plug-in architecture. I think the AJAX capabilities of jQuery are just one of the many plug-ins you could use.
The '$' that use see is also just a alias for calling the jQuery.
One last observation jQuery is defined as (from the jquery-1.4.2.js):
var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context ); },
In JavaScript, functions themselves are objects.
ReplyDeletevar x = function () {};
x.foo = "bar";
console.log(x.foo); // bar
EDIT:
To add onto this:
var x = function () {
return 'foo';
};
x.bar = function () {
return 'baz';
};
So now:
console.log(x()); // foo
console.log(x.bar()); // baz
Here's a Wikipedia article that explains it quite well:
ReplyDeletehttp://en.wikipedia.org/wiki/First-class_function
I believe the .ajax example is utilizing the jQuery plug-in architecture. I think the AJAX capabilities of jQuery are just one of the many plug-ins you could use.
ReplyDeleteThe '$' that use see is also just a alias for calling the jQuery.
One last observation jQuery is defined as (from the jquery-1.4.2.js):
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
},
and Ajax looks like:
jQuery.extend({
...some other goodness...
ajax: function( origSettings )
...more goodness...
});