Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the dot after dollar sign mean in jQuery when declaring variables?

Tags:

jquery

Functions in JavaScript are objects. And like most objects in JavaScript, you can arbitrarily add properties to them. The $ function is just that, a function. So if you want to pop a property onto it and reference a jQuery collection, or reference, you can.

By adding the collection as a property on the $ function, it is one less variable in the current scope. You can examine the keys of the jQuery function before and after if you'd like to see how it affects the function's topography and (enumerable) property list:

Object.keys($);
// ["fn", "extend", "expando"..."parseHTML", "offset", "noConflict"]

$.root = $("body");
// [<body>]

Object.keys($);
// ["fn", "extend", "expando"..."parseHTML", "offset", "noConflict", "root"]

$.root = $("body");

This adds a property to the $ functor (often referred to as the jQuery object, as $ == jQuery)

$root = $("body");

This adds a property to the 'global' scope, aka the window object in javascript. You can also refer the latter as

window.$root

The first one is creating a property called root in jquery and setting its value. The second one is only defining that the variable with name $root has the value on the right.