Вопрос

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?

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top