-1

Possible Duplicate:
Why would a JavaScript variable start with a dollar sign?

I am not sure what $scope and $defer are but they just seem to work.

function xyz($scope, $defer) {
    xyz = this;
    this.defer = $defer;
    this.scope = $scope;
    this.scope.test = function(re) {
        console.log(this,arguments);
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

2 Answers2

7

Generally these days devs name a variable $something to flag that it is an object type of a wrapping framework. For example, to cache a jQuery object, it makes sense to use $this = $(this);

That being said, there's nothing special about the dollar sign. Just a heads up for devs.

A bit of history and reasoning; ECMAScript 3 says:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

Whereas ECMAScript 5 says:

The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

So when someone says "Hey you're not supposed to use a dollar sign in your var cuz it's for MECHANICALLY-GENERATED code!" you can say "Psh, ECMA 5, hello?"

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
5

Those are names of parameters.

UPDATE: $ is perfectly valid (even by itself, which is probably more common thanks to certain JS libraries) as an identifier in JavaScript, but it isn't usually used as a sigil like this. I'm guessing the code was written by somebody with too much Perl experience, or possibly somebody who mostly deals with jQuery-based code.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • Or PHP even. I've caught myself doing it a couple times in the past. – AlienWebguy May 01 '12 at 16:04
  • Ive done it with variables that point to query object as a way to differentiate them. A practice I picked up while learning and it sort of stuck. – pdizz May 01 '12 at 16:59
  • @AlienWebguy Heh. I try to pretend that PHP doesn't exist, but a very good point ;-) – Hank Gay May 01 '12 at 18:15
  • @pdizz I don't write enough jQuery-based code for that to have come up as an issue for me, but if I were intermingling non-query objects with query objects often enough, that might be a nice way to help my future self figure out what is going on. – Hank Gay May 01 '12 at 18:18
  • @HankGay Oops. I meant 'jquery objects' not 'query objects' but yeah it's more organization and personal preference than anything – pdizz May 01 '12 at 19:03