Js Variable Starting With "@"
Solution 1:
This is totally valid:
var π = Math.PI;
This does not appear to be valid:
var@yourName = "Jamund";
This works though:
var$yourName = "Jamund";
If you're bored and want to learn all of the gory details: http://mathiasbynens.be/notes/javascript-identifiers
As for your specific problem, yeah it's probably either in a comment (JavaDoc uses @ in its comments and sometimes it's style has been used in JS comments) or it was meant to be processed and replaced server-side.
Solution 2:
Chapter 7.6 of ECMA-262, 5.1 edition defines what an identifier is. As @
is not allowed, you should not use it even if some browser may accept it. You should always strive for the broadest compatibility amongst all browsers if possible. Not using the @
in an identifier should not hinder you in any way.
IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
<ZWNJ>
<ZWJ>
Solution 3:
A single @
is usually a JSDoc parameter (if it's in a comment block). Wrapping the term with @
sounds like it could be a custom templating feature. But names preceded by @@
are usually native symbols. I don't believe you can actually use them in code, but they're often referred to in documentation that way.
E.g. Symbol.iterator
is referred to as @@iterator
, as in:
Array.prototype[@@iterator]()
TypedArray.prototype[@@iterator]()
String.prototype[@@iterator]()
Map.prototype[@@iterator]()
Set.prototype[@@iterator]()
Though in code you'd use it as such:
var myArray = [1, 2, 3, 4];
var it = myArray[Symbol.iterator]();
console.log(it.next().value); // 1console.log(it.next().value); // 2
You may also see references to:
@@match
-RegExp.prototype[@@match]()
@@replace
-RegExp.prototype[@@replace]()
@@split
-RegExp.prototype[@@split]()
@@search
-RegExp.prototype[@@search]()
@@species
-Map[@@species]
,Set[@@species]
@@toPrimitive
-Date.prototype[@@toPrimitive](hint)
,Symbol.prototype[@@toPrimitive]()
@@unscopables
-Array.prototype[@@unscopables]
Solution 4:
π is a standard Greek character that is used in Greek words so that makes it valid to use for a variable name. The rules are that you can use $ _ or any word character except the JavaScript keywords. However, you can use forbidden keywords including an empty string if you use an accessor: var forbidden ={}; forbidden[""]="hello";
you can even do this on the global object:window['@weird name for a variable'] = "There are very few good reasons to do this!!"
but just because you can doesn't necessarily mean you should! Variables named this way are the same as any other var the only restriction is that you have to use the Square brackets with a string to access these values.
Post a Comment for "Js Variable Starting With "@""