How does jQuery achieve making $ an alias for the jQuery function?

后端 未结 4 2165
一生所求
一生所求 2020-12-14 09:00

I’m having a little trouble wrapping my head around the $ sign being an alias for the jQuery function, particularly in a plugin. Can you explain how jQuery achieves this ali

4条回答
  •  死守一世寂寞
    2020-12-14 09:09

    A function, like any object in javascript, can be assigned to a variable. This variable can have any name (that follows the JS variable naming rules). "$" satisfies the naming rules, so the jQuery function is aliased to "$" for brevity. Consider the following example:

    var myFn = function() { alert('myFunc'); };
    var $ = myFn;
    
    $();
    // alerts 'myFunc'
    

提交回复
热议问题