惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
H
Hacker News: Front Page
C
Cybersecurity and Infrastructure Security Agency CISA
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
AI
AI
G
GRAHAM CLULEY
IT之家
IT之家
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
D
Docker
Recent Announcements
Recent Announcements
O
OpenAI News
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
Troy Hunt's Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic

Mathias Bynens

A horrifying globalThis polyfill in universal JavaScript JavaScript engine fundamentals: optimizing prototypes JavaScript engine fundamentals: Shapes and Inline Caches Asynchronous stack traces: why await beats Promise#then() ECMAScript regular expressions are getting better! Unicode property escapes in JavaScript regular expressions ES2015 const is not about immutability Unicode-aware regular expressions in ES2015 Dear Google, please fix plain text emails in Gmail PBKDF2+HMAC hash collisions explained JavaScript has a Unicode problem Processing Content Security Policy violation reports Hiding JSON-formatted data in the DOM with CSP enabled Loading JSON-formatted data with Ajax and xhr.responseType='json' Reserved keywords in JavaScript How to support full Unicode in MySQL databases How to speedrun Dropbox’s Dropquest 2012 Unquoted font family names in CSS Unquoted property names / object keys in JavaScript Valid JavaScript variable names in ES5 CSS character escape sequences JavaScript’s internal character encoding: UCS-2 or UTF-16? The smallest possible valid (X)HTML documents JavaScript character escape sequences JavaScript foo.prototype.bar notation Ambiguous ampersands HTML element + attribute notation How I detect and use localStorage: a simple JavaScript pattern Unquoted attribute values in HTML and CSS/JS selectors The end-tag open (ETAGO) delimiter Using the oninput event handler with onkeyup/onkeydown as its fallback Everything you always wanted to know about touch icons In defense of CSS hacks — introducing “safe CSS hacks” AirPlay video support in iOS Safari — a bookmarklet Completing Dropbox’s Dropquest 2011 in 60 seconds Using CSS without HTML How to create simple Mac apps from shell scripts Using setTimeout to speed up window.onload Bulletproof JavaScript benchmarks Thoughts on Safari Reader’s generated HTML How to enable Safari Reader on your site? The XML serialization of HTML5, aka ‘XHTML5’ The id attribute got more classy in HTML5 The three levels of HTML5 usage The HTML5 document.head DOM tree accessor Bulletproof HTML5 <details> fallback using jQuery Displaying hidden elements like <head> using CSS Inline <script> and <style> vs. external .js and .css — what’s the size threshold? Using Showdown/PageDown with and without jQuery
Valid JavaScript variable names in ES2015
Mathias · 2015-03-04 · via Mathias Bynens

ES2015 updates the grammar for identifiers. This affects a number of things, but most importantly, identifiers can be used as variable names, and identifier names are valid unquoted property names. This post describes the observable changes compared to the old ES5 behavior.

Reserved words

ES2015 reserves the await keyword in module contexts for use in the future.

// Valid in ES5, but invalid in an ES2015 module context:
var await;

Escape sequences

The only type of escape sequence allowed in ES5 identifiers is the so-called Unicode escape of the form \uXXXX.

In ES2015, the new Unicode code point escape syntax is accepted as well.

// Valid in ES5 and ES2015:
var a;

// Valid in ES5 and ES2015:
var \u0061;

// Invalid in ES5, but valid in ES2015:
var \u{61};

Acceptable Unicode symbols

In ES2015, identifiers must start with $, _, or any symbol with the Unicode derived core property ID_Start.

The rest of the identifier can contain $, _, U+200C zero width non-joiner, U+200D zero width joiner, or any symbol with the Unicode derived core property ID_Continue.

This differs from the definition for ES5 identifier names that was based on Unicode categories. Consequently, some Unicode symbols that were disallowed in ES5 identifiers can now be used in ES2015 identifiers just fine, and vice versa.

// Valid in ES5 & Unicode v5.1.0+, but invalid in ES2015:
var ⸯ; // U+2E2F VERTICAL TILDE
var \u2E2F; // U+2E2F VERTICAL TILDE

In ES5 identifiers, astral symbols were disallowed, even when represented as an escaped surrogate pair (\uXXXX\uXXXX).

In ES2015, astral ID_Start or ID_Continue symbols in identifiers are accepted when represented as a raw symbol or using a single \u{…} escape sequence.

// Invalid in ES5, but valid in ES2015:
var 𐊧; // U+102A7 CARIAN LETTER A2
var \u{102A7}; // U+102A7 CARIAN LETTER A2

// Invalid in ES5 and ES2015:
var \uD800\uDEA7; // U+102A7 represented as a surrogate pair

The ES5 spec allowed implementations to base their identifier support on Unicode versions as old as Unicode v3.0.0. ES2015 lists Unicode v5.1.0 as the minimum Unicode version required for compatibility.

// Valid in ES5, but only works in some ES5 engines (i.e. those with Unicode
// data from v3.2.0 or more recent):
var Ƞ; // U+0220 LATIN CAPITAL LETTER N WITH LONG RIGHT LEG
var \u0220;
// On the other hand, it is guaranteed to work in all ES2015-compliant engines.

// Valid in ES5, but only works in some ES5 engines (i.e. those with Unicode
// data from v4.0.0 or more recent):
var ȡ; // U+0221 LATIN SMALL LETTER D WITH CURL
var \u0221;
// On the other hand, it is guaranteed to work in all ES2015-compliant engines.

// Valid in ES5, but only works in some ES5 engines (i.e. those with Unicode
// data from v5.1.0 or more recent):
var _҇; // U+0487 COMBINING CYRILLIC POKRYTIE
var _\u0487;
// On the other hand, it is guaranteed to work in all ES2015-compliant engines.

No more non-standard behavior

At some point all major JavaScript engines supported reserved words as identifiers if at least one of the characters was escaped. For example, var var; wouldn’t work, but e.g. var v\u0061r; would — even though this was never part of the spec.

// Invalid in ES5 and ES2015:
var var;

// Invalid in ES5 and ES2015, but supported in old ES5 engines:
var v\u0061r;

ES2015 explicitly makes this behavior non-conforming, and implementations are moving away from it. Firefox/Spidermonkey, Safari/JavaScriptCore, and IE/Chakra have already dropped this behavior; Chrome/Opera/V8 plan to.

Bugs

There are open bug tickets to fully implement the ES2015 identifier grammar in Chrome/Opera/V8 (now fixed), Firefox/SpiderMonkey, Safari/JavaScriptCore (#121541 (now fixed), #208998), Microsoft Edge/Chakra (now fixed), Acorn (and therefore Babel) (#214 (now fixed), #215), Esprima, and Traceur.

Resources

I wrote some identifier tests based on ES2015 and Unicode 5.1.0, i.e. the minimum required Unicode version as per the spec. They helped me find bugs in several engines.

I created a script that generates a regular expression matching only valid identifiers as per ES5 and ES2015. At the time of writing, the ES5 version is being used in the Esprima and Acorn parsers, among other open-source projects.

There’s also an online JavaScript identifier validator, a tool that makes it easy for you to check if a given string is a valid variable name in JavaScript.

JavaScript variable name validator

I’ve updated the unquoted JavaScript property name validator accordingly.

JavaScript unquoted property name validator