How does jQuery makes the jQuery object both a function and an object property?

后端 未结 3 353
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 05:23

I\'ve been wondering for a long time how jQuery can be both a function and an object property.

You can use it like a function, jQuery(...) and you can use it like a

相关标签:
3条回答
  • 2020-12-06 06:07

    I think the concept of what exactly is jQuery in terms of code concepts is quite confusing. I ran across this link, which explains the jQuery Architecture in a very simple and easy manner : http://blog.mikecouturier.com/2010/02/beginning-with-jquery-solid-foundation_22.html

    So, in short, $ is an alias for a JavaScript function called 'jQuery', and methods invoked by using a dot notation like $.trim ( ) are static methods on the 'JavaScript 'jQuery' function. Note that a function inherits from object in JavaScript, and so a function is an object in JavaScript.

    0 讨论(0)
  • 2020-12-06 06:29

    functions are objects in javascript. So you can have your main function

    var $ = function() {  alert('wat'); }
    

    and then extend it

    $.fadeTo = function() { alert('fadeto'); }
    
    0 讨论(0)
  • 2020-12-06 06:30

    Because in JavaScript, functions are objects that can have properties:

    function test() { ... }
    
    test.a = function () { ... };
    test.a(); // totally valid
    
    0 讨论(0)
提交回复
热议问题