Netside
Netside

Reputation: 80

How does JQuery utilize $ without a function by this name?

I'm currently learning JS and was dissecting the JQuery source code for practice.

I understand JQuery functions are accessible using the $ sign because $ is assigned to the main function used: JQuery(). However, when I comb through the source code, I cannot find a function named $ anywhere, and can only find function JQuery() or references to window.$. I realize JQuery utilizes JS's prototype function pretty extensively, so maybe that's why I'm not seeing it?

Can someone please point me to whichever line is making JQuery accessible via the dollar sign?

Thanks. I realize this is probably an amateur question.

Version of JQuery used: https://code.jquery.com/jquery-3.1.0.js

Upvotes: 0

Views: 48

Answers (2)

Surajit Biswas
Surajit Biswas

Reputation: 809

$ is just a normal variable. You can do var $ = 12123

Something like ...

var $ = function () {
  // some code
}

Upvotes: 1

Jacob
Jacob

Reputation: 78920

It's these lines:

if ( !noGlobal ) {
    window.jQuery = window.$ = jQuery;
}

In a browser environment, anything inside of window is global, so that's why $ is globally accessible.

Upvotes: 4

Related Questions