Question

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?

Était-ce utile?

La solution

$ 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.$

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top