user1588174-Andy
user1588174-Andy

Reputation: 21

Javascript: When to use a dollar sign before a variable name

I cannot figure out the meaning of putting a dollar sign before certain variables.

For example: $(document).ready
Another example: (function ($) {$.fn.aToast = function (options) {...

Here is example code I found on the internet:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/xhtml;charset=UTF-8" > 
        <title>jQuery test 2</title>  <h1>jQuery test 2</h1><hr>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    </head>
    <body>
        <button>TEST1</button>
        <input type='button' 
            data-aToast='true' 
            data-aToast-text='TEST2 was pressed' 
            value='TEST2' 
        />
        
        <div style='display:inline-block'><br>
        Here is Normal text in a <b>&lt;div&gt;</b></div>

        <script>
            (function ($) {
                $.fn.aToast = function (options) {

                var $this = $(this),
                settings = $.extend({
                    fadeOut: 400,
                    fadeIn: 400,
                    delay: 3000,
                    text: 'TEST1 was pressed',
                    toastListenEvent: 'click',
                    aClass: false
                }, options),
                $at = false,
                aTevents = false;

                settings.toastListenEvent = settings.toastListenEvent + ' a-Toast';
                settings.aClass = 'aToast-view-message' + (settings.aClass ? ' ' + settings.aClass : '')
                if ($('[data-aToast=true]:not(.aToast-init)').length) {
                    $this = $this.add($('[data-aToast=true]:not(.aToast-init)').addClass('aToast-init'));
                }
               
                function _remove() {
                    $(this).stop().remove();
                }
               
                function _displayDiv() {
                    $('.aToast-view-message').hide();
                    var $div = $('<div/>', {
                        text: $(this).data('atoast-text') ? $(this).data('atoast-text') : settings.text,
                        class: settings.aClass
                    }).stop().fadeIn(settings.fadeIn)
                        .delay(settings.delay)
                        .fadeOut(settings.fadeOut, _remove)
                        .appendTo('body');
               
                    console.log($div[0]);
                }
               
                $this.not('[data-aToast=false]').on(settings.toastListenEvent, _displayDiv);
               
            };
        }(jQuery));

        $('button').aToast({
            aClass: 'x123' // users-dont-care-about-this-class-name'
        });

        $('div').aToast({
            aClass: 'Toast class',
            toastListenEvent: 'mouseenter',
            text: 'ERROR ... '
        });

        </script>
    </body>
</html>

Upvotes: 2

Views: 1318

Answers (1)

Kaslai
Kaslai

Reputation: 2505

There is no particularly special meaning to the $ symbol in Javascript. It is treated neither as an operator character nor as a numeric character, which means that you may use it in names without any special conditions.

That said, jQuery defines a function called $ that you may use to perform jQuery actions upon an object. $(this) will just create a jQuery object wrapper around the this object so that you can perform jQuery actions on it. $('foo') will create a jQuery object that references all instances of foo in your document, and $('#thing') will create one that references the object with the ID of thing.

Read up on jQuery here

Upvotes: 3

Related Questions