How is jQuery's $ a function and an object?

前端 未结 8 821
名媛妹妹
名媛妹妹 2020-11-29 08:22

I mean object as in {} [object Object]. How does it do $(selector) and $.fn.init at the same time?

Can you give me a simple ex

8条回答
  •  渐次进展
    2020-11-29 08:45

    jQuery or $ is an function(you know $ is an alias of jQuery).

    // Define a local copy of jQuery
    jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context, rootjQuery );
    },
    

    In Javascript, everything is an object even function too. Hence You can directly add properties to function.

       jQuery.find = function () {
       }
    

提交回复
热议问题