How is the “jQuery” var a function and an object?

后端 未结 2 1630
孤城傲影
孤城傲影 2021-01-11 11:52

For example, when you use jQuery(\'someDiv\');, it\'s a function, but you can also use jQuery.ajax(...);.

How is it possible?

2条回答
  •  旧巷少年郎
    2021-01-11 12:37

    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 );
        },
    

    and Ajax looks like:

    jQuery.extend({
    ...some other goodness...
    ajax: function( origSettings ) 
    ...more goodness...
    });
    

提交回复
热议问题