Skip to content

Instantly share code, notes, and snippets.

@subzey
Created October 3, 2012 22:19
Show Gist options
  • Save subzey/3830251 to your computer and use it in GitHub Desktop.
Save subzey/3830251 to your computer and use it in GitHub Desktop.
UnicodeEscapeSequence in Identifier

According to ECMA 262 both versions 3 and 5 variable name may contain chars escaped by \uHHHH. So var ы; and var \044b; is essentially the same.

The Syntax section there also states that variable name cannot be equal to any of the reserved words. So var this; is surely invalid.

But what happens if var \0074his is encountered?

Specs doesn't clearly explain this. "Unicode escape sequences are also permitted in an IdentifierName, where they contribute a single character to the IdentifierName...". Great, but does it mean that escape sequence must be unescaped prior to comparsion with reserved kerwords or not?

In other words, is \0074his the same as this?

So, I've created a test in order to find out how actual JS engines handle this.

var testResult;
try {
  testResult = Function('with({"this": "variable"}){return \\u0074his;}').call("keyword");
} catch (e) {
  testResult = 'error';
}

(Please note that backslash itself is escaped)

If \0074his is a normal variable name, testResult gets value from property called this in object within with.

If it's not, Function throws SyntaxError and it is caught by try...catch.

If \0074his is parsed as keyword this, testResult is equal to String object passed in call (it will be coerced to object anyway upon applying as this, so I decided not to bloat the code).

Here's the first results:

Chrome (V8) and Opera treats \u0074his as a normal variable. So var name in these engines is compared with keywords before unescaping.

Firefox treats \u0074his as a keyword. I cannot explain this as there's no escaping for keywords mentioned in spec at all.

(IE to be tested)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment