0

I've got the following code snippet:

$Core.box = function($sRequest, $sWidth, $sParams) {
    tb_show('', $.ajaxBox($sRequest, 'width=' + $sWidth + ($sParams ? '&' + $sParams : '')));   
    return false;
};

but I'm not understanding what does $Core stand for.

What's the meaning of the dollar sign in $Core and the other variables? Why is the function code stored into $Core.box?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 3
    Often `$` is used to prefix variables that represent `jQuery` result sets. But your code looks like a php developer wrote some JS code using the `$` prefix for all the variables. – t.niese Dec 31 '14 at 12:03
  • 2
    also answered here http://stackoverflow.com/questions/10787342/why-does-jquery-have-dollar-signs-everywhere - jQuery is the jQuery object itself, its a short hand. – Lars Anundskås Dec 31 '14 at 12:04
  • @t.niese:The file containing the above code is not PHP file. It's a .js file – PHPLover Dec 31 '14 at 12:04
  • Without seeing more of the code, it looks like `$Core` is juste a variable which its name starts whith a dollar. Sometimes this notation is used to differentiate "jQuery objects" from others among variables. @LarsAnundskås > you're answering ontly the question title, the question self is different from what you might expect – Laurent S. Dec 31 '14 at 12:05
  • The `$` is just a valid character in identifiers (ie. variable names, function names); `a$b = 42;` is perfectly valid in JS, for example ... The `$` has no special meaning to the language. – Martin Tournoij Dec 31 '14 at 12:05
  • valid but generally bad practice :) – Lars Anundskås Dec 31 '14 at 12:06
  • in your code it means nothing...., except for the $.ajaxBox .. – Gogol Dec 31 '14 at 12:06
  • Might also be related to [this](https://github.com/Munawwar/lithium-ui/wiki/Tutorial-1-Hello-World), where the user tries to access the `Box` package from the `lib/lui/core/Box` directory. – Jean-Paul Dec 31 '14 at 12:07
  • $ is a shortcut that is used by many JavaScript libraries, not just jQuery. I would recommend looking at what other libraries are used in the environment/page as there might be a conflict between jQuery and another library that are both trying to use $ as a shortcut. – John Dec 31 '14 at 12:07
  • @PHPLover I didn't say its is a php file. I just said it looks like it is JavaScript code that was written by a person that is used to write PHP code. – t.niese Dec 31 '14 at 12:08

2 Answers2

4

The $ sign means nothing particular in JavaScript, it is only another valid character to use in variable names.

For example you can call a variable abc, a$b$c, hèy or wathever you like, there's no difference, the only thing that changes is the name of the variable:

var abc = 1,
    a$b$c = 2,
    hèy = 3;

console.log(abc, a$b$c, hèy); // 1 2 3

Why the dollar sign then? The dollar sign is just a shorter, more convenient name for the jQuery function.

window.jQuery = window.$ = jQuery;
// This is usually what a jQuery library does

In fact, using the jQuery library it will happen to call jQuery(...) several times, and writing $(...); $(...); $(...); is much faster than writing jQuery(...); jQuery(...); jQuery(...);. You can actually see that typing the following lines in the console of a site that uses jQuery, like Stack Overflow itself, will return a true value:

$ === jQuery           // true
$.bind === jQuery.bind // true
// and so on...

Then why do users put a $ at the beginning of variable names? Since that the common abbreviation for jQuery is the dollar sign, users tend to name variables created using jQuery with a dollar sign at the beginning, so it will be easy to know where they come from later in the code, like this:

var body = document.body,
    $body = $(document.body); // equivalent to jQuery(document.body);

// Now you know that $body does have all the common jQuery collection properties, but body doesn't

Referring to your code: the $Code variable is just an object like any other, but it has been probably created using jQuery.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
1

$Core is just a global variable/object (actually it is the same as window.$core in that scope).

$ is a valid prefix for variable names, although one standard is only to use that prefix for jQuery objects/variables.

$Core.box = function(){} is simply assigning a function to a box property of that global variable.

It is likely that function is used by a library or plugin, but without more information about your page and what is included, it is not clear exactly what it is used for :)

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202