Wednesday, December 21, 2011

Javascript - $ Symbol

This is a question for me when I am working with JQuery. What is a $.XXX function? Is $ a variables or object?

In Javascript, $ is commonly use as a short hand for variable definition and function declaration. In layman term, it is just a name.

Javascript variables must start with a letter, underscore or dollar sign.

Thus, we can have a function variable like

var $ = function(){
          alert("$ function");
}

Then, you can call this function as $()

 onclick="$()"

Now, back to jQuery, by default, jQuery uses $ as a shortcut for "jQuery"

That means

jQuery('div.foo'); === $('div.foo');

More readings:

http://www.authenticsociety.com/blog/JavaScript_DollarSign
http://docs.jquery.com/Using_jQuery_with_Other_Libraries

No comments:

Post a Comment

Sed - Example guide

sed is a very useful stream editor to perform search and replace. Below are some useful tip 1. Usage sed 's/apple/orange/' file...