Romantic Electron
Romantic Electron

Reputation: 779

How does jQuery library use $ in place of "JQuery" to reference objects?

I want to have a shorthand like the $ symbol as used by JQuery for quick reference to an object that is widely used throughout my code.

How can it be done?

Upvotes: 3

Views: 396

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

$ in jQuery is just a JavaScript variable name;

var $ = 5;

Or if you want to add functionality

var $ = {};
$.myMethod = function(){ 
    console.log("Hello");
};

See this book section about namespacing and modules.

What jQuery does in its source code:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

They just assign an object reference to the object jQuery to window.$

Upvotes: 5

Related Questions