0

I've come across someone's old code that has variables identified like: @user_id@, @reference_id@, and so forth. Wikipedia says "In certain JavaScript implementations, the at sign (@) can be used in an identifier..." In what JS implementations does this work? I can't get it to work.

4
  • 2
    are you shure it wasn't a generated comment that explains something (functionality,arguments) about a certain function? Commented Apr 24, 2012 at 18:58
  • 4
    It's possible they were replacing those tokens server-side. Commented Apr 24, 2012 at 18:58
  • 1
    Even if it works in some random JS engine, it doesn't matter because it won't work in most of them. Don't do this. Commented Apr 24, 2012 at 18:59
  • It's definitely not a comment, though it could be replaced server side -- Thanks for the ideas! Commented Apr 24, 2012 at 20:04

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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>

Comments

2

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); // 1
console.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]

Comments

0

π 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.