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();
2

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

$, Jquery is an object or a function ?
@Cris: In JavaScript, every function is an object and, thus, may have properties.
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(){}.

Comments

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.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.