Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @ do in JavaScript?

Tags:

javascript

I've tried searching all over the place and even read through some chunks of the ECMA specification, and I cannot for the life of me find anything to explain what the at symbol (@) does in JavaScript. Can anyone out there please help me understand this little guy?

Doc I was reading : http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

Thanks for any help.

like image 857
Cranium Slows Avatar asked Mar 28 '11 20:03

Cranium Slows


People also ask

What is '$' in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).

What does += mean?

The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable.


2 Answers

You won't find it in ECMA-262. You want ECMA-357.

§ 11.1.1 Attribute Identifiers

An AttributeIdentifier is used to identify the name of an XML attribute. It evaluates to a value of type AttributeName. The preceding “@” character distinguishes a XML attribute from a XML element with the same name. This AttributeIdentifier syntax was chosen for consistency with the familiar XPath syntax.

like image 138
Eli Grey Avatar answered Oct 31 '22 07:10

Eli Grey


@ doesn't do anything in ECMA262. The document uses it as a placeholder for different operators that use the same syntax. For example, they describe compound assignment such as foo @= bar instead of indicating all of the compound assignment operators.

Compound assignment operators are those such as x += 1, except there are quite a few of them, thus, use of the @ sign to represent all of them symbolically in the manual only, nothing in code.

As Eli Grey points out, this is a feature that exists in EcmaScript for XML, often nicnamed E4X, offically named ECMA-357.

It's important to note that E4X support is presently not common everywhere, and not really the most suitable standard "for the open web", as the WikiPedia article states:

E4X is supported by Mozilla's Rhino, used in OpenOffice.org and several other projects, and SpiderMonkey, used in Firefox, Thunderbird, and other XUL-based applications. It is also supported by Tamarin, the JavaScript engine used in the Flash virtual machine. It is not supported by Nitro (Safari), V8 (Google Chrome), Opera, nor Internet Explorer.1

From the E4X example on the page,

You can assign a variable some XML data (in much the same way we can use JSON syntax to assign objects)

var sales = <sales vendor="John">
    <item type="peas" price="4" quantity="6"/>
    <item type="carrot" price="3" quantity="10"/>
    <item type="chips" price="5" quantity="3"/>
  </sales>;

Then, to get an attribute value, such as the value of "vendor", we can do

alert( sales.@vendor );

And get back "John" in the alert box.

like image 41
Incognito Avatar answered Oct 31 '22 07:10

Incognito