javascript function what is scope and why using $

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);
    }
}

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?"


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.

链接地址: http://www.djcxy.com/p/94956.html

上一篇: 为什么“$”是一个有效的函数标识符?

下一篇: javascript函数什么是范围和为什么使用$