最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS variable starting with "@" - Stack Overflow

programmeradmin5浏览0评论

I've e 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.

I've e 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.

Share Improve this question asked Apr 24, 2012 at 18:56 m0therwaym0therway 4311 gold badge4 silver badges13 bronze badges 4
  • 2 are you shure it wasn't a generated ment that explains something (functionality,arguments) about a certain function? – gion_13 Commented Apr 24, 2012 at 18:58
  • 4 It's possible they were replacing those tokens server-side. – simshaun 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. – user578895 Commented Apr 24, 2012 at 18:59
  • It's definitely not a ment, though it could be replaced server side -- Thanks for the ideas! – m0therway Commented Apr 24, 2012 at 20:04
Add a ment  | 

4 Answers 4

Reset to default 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 ment (JavaDoc uses @ in its ments and sometimes it's style has been used in JS ments) or it was meant to be processed and replaced server-side.

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 patibility 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>

A single @ is usually a JSDoc parameter (if it's in a ment 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]

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

发布评论

评论列表(0)

  1. 暂无评论