-1

Which library of JavaScript having definition of selector '$' ? How it becomes globally accessible ??? Can we write a new operator ?

dinesh kandpal
  • 738
  • 7
  • 16
  • Jquery is the library which has $ selector. – Vaibhav Singh May 10 '17 at 04:09
  • Yes, you can define `$` as a variable. – guest271314 May 10 '17 at 04:10
  • You need to add the CDN link in your html file to make it globally available – Vaibhav Singh May 10 '17 at 04:10
  • I am asking the name of the library ? How can we define other variables so that they will be globally accessible like '$' !! @VaibhavSingh – dinesh kandpal May 10 '17 at 04:11
  • @VaibhavSingh `$` is not a selector – guest271314 May 10 '17 at 04:11
  • if iam not wrong, you want to change $ sign to some other sign ? use $.noConflict() read more -> https://api.jquery.com/jquery.noconflict/ – Developer May 10 '17 at 04:15
  • 3
    The dollar sign `$` has no special meaning in JS, it is simply one of various characters that may be used in variable names. [Several popular libraries declare a function with the name `$`.](http://stackoverflow.com/questions/4632041/what-javascript-libraries-are-known-to-use-the-global-dollar-sign-window). It becomes globally accessible the same way a function with any other name does. – nnnnnn May 10 '17 at 04:17
  • No, First of all i want to know where '$' define ? What makes it globally accessible ? Can we define some new operators like this "Globally accessible" if yes then how ? – dinesh kandpal May 10 '17 at 04:17
  • *"where '$' define"* - Download and open jquery.js, and search through the code for `window.$`. – nnnnnn May 10 '17 at 04:18
  • '$' is defined in the jquery library @dineshkandpal – Vaibhav Singh May 10 '17 at 04:19
  • If you want to define "$" like global variables, you can do this window.var_name='something' – Vaibhav Singh May 10 '17 at 04:22
  • 1
    *"Can we define some new operators like this"* - **It is *not* an operator**, it is a function name. Operators are things like `+`, `-`, `&&`, etc. You cannot define new operators, but you can define as many functions as you like. – nnnnnn May 10 '17 at 04:22
  • I think we wants to define global variable like '$' is for jquery. – Vaibhav Singh May 10 '17 at 04:24

1 Answers1

3

$ is a valid variable name in JavaScript. So you free to write something like

var $ = document.querySelectorAll.bind(document);
var elements = $('a.link');

In practice, $ is an alias of a jQuery function from the corresponding library.

artptr
  • 254
  • 2
  • 9
  • jQuery or Mootools it is, and will conflict with eachother which can be fixed by f.e.: `jQuery.noConflict();` – SurudoiRyu May 10 '17 at 13:45
  • @artptr I want to ask Can we define our own variable that represent whole library ? as $ represent can we override existing $ sign with our our sign as ^,# ? – dinesh kandpal Sep 25 '18 at 11:11
  • @dineshkandpal You can't. Only $ and _ chars may be present in a variable name. So $ widely associated with jQuery, while _ is associated with Underscore or Lodash. But you still can use them for your own purposes. – artptr Sep 25 '18 at 21:54