1

Below declaration of $. is meant to be a javascript variable ?

I understand how to use $ for selectors

$('#Text').click(function () {
  $('#Text').css('color', 'red')
});

But when I declare variable I was used to declare that as var x = function(){}...

What this notation means "$." is a jquery way of declaring variables ?

$.searchTwitter= function(){


                    $.ajax({
                        url:"http://search.twitter.com/search.json",
                        data:{
                                q:'dogs'
                        },
                        dataType:'jsonp',
                        success:function(results){
                            console.log(results);
                        }
                    });
               };

 $.searchTwitter();
Cris
  • 4,947
  • 6
  • 44
  • 73

6 Answers6

3

In jQuery, $ is an alias for jQuery.

i.e. $.ajax(); works same as jQuery.ajax();

In Javascript, $ is nothing special; just simply a valid JavaScript identifier.

Ref: https://learn.jquery.com/about-jquery/how-jquery-works/

Note: The jQuery library exposes its methods and properties via two properties of the window object called jQuery and $. $ is simply an alias for jQuery and it's often employed because it's shorter and faster to write.

mmuzahid
  • 2,252
  • 23
  • 42
2

$ is jQuery itself. It's declared in the jQuery library. When you use $.searchTwitter = function(){} what you're really saying is JQuery.searchTwitter = function(){}.

Saar Kuriel
  • 572
  • 4
  • 16
1

$ is known as Syntactic Sugar

In case of jQuery it is jQuery Object. What that means is that, using $("#mydiv") or jQuery("#mydiv") is the same. $ only eases the pain of writing the full object name jQuery.

If you want to know more

Mihir
  • 3,812
  • 3
  • 25
  • 29
1

$ is a global variable defined by jQuery and an alias for the variable jQuery. By assigning $.searchTwitter = function() { ... } one adds a new method to the global jQuery object.

Practically, there is no advantage or disadvantage of such a declaration in contrast with the usual function searchTwitter() { ... } syntax. It is sometimes used to declare functions which are related to jQuery or are a plug-in for jQuery.

Callidior
  • 2,899
  • 2
  • 18
  • 28
1

The dollar sign is one of the chars allowed in variable names in Javascript, along with numbers, underscores and English letters.

Examples for 'special' variable names are:

$$ $_ $ _

Dot notation is a way to assign a property to an object.

Example:

var x = { myName: 'Roman' };
x.searchTwitter = function() {}; 
x.searchTwitter();
console.log(x.myName);

When a browser loads the Jquery library, Jquery declares a global variable named Jquery and also declares an alias for it - the $.

Answer your question is no, it's not a special way of Jquery declaring variables. It's standard Javascript way of adding properties to an object.

funfan
  • 66
  • 3
0

For safety reasons I like to wrap my code with closure function with $ as argument. See code below:

(function($){
  
 $.searchTwitter = function(){

    $.ajax({
      url:"http://search.twitter.com/search.json",
      data:{
        q:'dogs'
      },
      dataType:'jsonp',
      error: function (xhr, status, error) {
        $('#errors').html(JSON.stringify(xhr));
      },
      success:function(results){
        console.log(results);
        $('#results').html(JSON.stringify(results));
      }
    });
 };
 $.searchTwitter();

})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="results"></div><div id="errors"></div>

but Cris this Twitter API is deprecated. It should not work.

enter image description here

num8er
  • 18,604
  • 3
  • 43
  • 57