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

前端 未结 8 817
名媛妹妹
名媛妹妹 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:55

    $ is a function. a method of $ can return any thing.

    For example:

    $ = function() {
         return {
                     foo : function() { return 'baa'; },
                     r1: 1,
                     r2 : 'string'
                }
    };
    
    typeof $ <- function 
    typeof $() <- object
    typeof $().foo <- function 
    typeof $().foo() <- string
    typeof $().r1; <- number 
    typeof $().r2 <- string
    

提交回复
热议问题