Var @foobar



  • I was doing some refactoring of an old javascript, and found out that @ and # have some special meaning. I don't kow what they are. Googling for '@' is tricky. Does anyone know?



  • http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf doesn't list it except as a placeholder for math ops, and in the context of escape and URIEncode. Dunno.



  • http://msdn.microsoft.com/en-us/library/ce57k8d5(v=VS.85).aspx Also not listed as an operator in JScript (which is 99.9% ECMAScript, but might as well check)



  • I know the char limit is a pain, but what type of special meaning are you seeing? Are you sure it's not just part of the variable name? Or ignored entirely?



  • In firefox, @variable produces "missing variable name", and #foobar causes "illegal character". Under chrome, they both produce "illegal character"



  • IE8 uses @ for conditional comments. # is an illegal as well. I'll go read your links. Forgot to mention they cause errors. :)



  • Is it in a .js file? The rules are different... nothing in JavaScript prohibits them, but something in HTML/XHTML/XML/whatever might. I doubt either is legal in var names though.



  • @blakeyrat said:

    Is it in a .js file? The rules are different...
    No difference. It's academic now because I've switched to the _, but it's still weird.



  • @dhromed said:

    @blakeyrat said:
    Is it in a .js file? The rules are different...
    No difference. It's academic now because I've switched to the _, but it's still weird.

    Now that the post limit is (according to General Discussion) fixed, care to explain a little bit more on this? I'm still confused-- how was it working before?

    The only thing I can think of is that if they are eval-ing the function, they could do a little pre-processing to turn the @ and # into something... but surely you'd have noticed that.



  • I was writing a class for a graph writer thing, and thought to myselfit might beneficial if I denoted private properties (i.e. the class-function's local variables) with a prefixed @ or #.

    Those characters, to the best of my knowledge, have no special meaning in JS. And yet, they produce syntax errors.

    Just put it in a console and see for yourself.



  • Ok, silly me. When you typed "refactoring old JavaScript", I stupidly assumed you meant that you were refactoring old JavaScript instead of writing a brand new class. And when you said that @ and # have some "special meaning", I stupidly assumed you'd observed some special meaning of those characters instead of just syntax errors.

    If you re-read those specs (well the ECMA one at least), you'll probably find it defines a set of valid characters for identifiers, with all other characters being invalid. Which would be why you get a syntax error, and also why a simple search for @ and # doesn't show up in the spec. I'm virtually certain that's the case here.



  • Yeah, might be a case of "just cuz".

    I'm not sure I can come up with a reason why $ and _ were included in the list of valid chars, and everything else wasn't— even if they are unused.



  • @dhromed said:

    I'm not sure I can come up with a reason why $ and _ were included in the list of valid chars, and everything else wasn't— even if they are unused.

    That way lies madness (and future unextensibility).



  • assert(_this instanceof $parta, true);


Log in to reply