科技和互联网

比较Javascript ECMAscript

在网络上,我们总会看到 JavaScript已经发展到了ES5,ES6,这到底代表着什么?从ES6又有另外的名称ES2015,这又是什么意思?这篇科普短文,通过比较Javascript ECMAscript,带我们认识它们。ECMA是一个国际标准化组织,为一般用途的脚本开发语言制定标准, 现在最新的标准是2017年6月发布的ECMA-262。脚本语言是一种计算机代码,被定义为用于设计在现有的系统上执行特定的动作的代码语言集,而在最新的ECMA-262标准里面,ECMA为一般用途的脚本语言的制定了规范,符合这种规范的脚本语言都属于ECMAscript。JavaScript是一种一般用途的脚本语言,它遵守ECMAscript的规范。通过阅读ECMAscript,我们会知道怎样去创造一个脚本语言,通过阅读JavaScript文档,我们会知道怎样去运用这个脚本语言。

JavaScript引擎是一种软件环境,是用来解释和执行Javascript语言的解释器。JavaScript引擎有很多种,比如最著名的v8 Chrome,Firefox的SpiderMonkey,…… 每一种引擎都支持特定的一个JavaScript语言的子集,每种引擎对脚本语言的认识和解释的工作方式不同,所以导致了在速度上会有所差异。JavaScript runtime运行库,是指一种让JavaScript代码在里面执行的环境,runtime提供了JavaScript能够运行和工作的对象。ES6是指ECMA-262第6版本的标准,它对于ECMAscript规范有的重大变革,并且从ES6开始,要用年代来命名。Babel是把ES6代码转换成ES5代码的中间件,因为新的用ES6开发的外部应用有可能被运行在不能兼容的老式浏览器比如Internet Explorer里,所以才有存在这个工具的必要。

原文链接:https://medium.freecodecamp.org/

抄录:

What’s the difference between JavaScript and ECMAScript?

Michael Aranda

I’ve tried googling “the difference between JavaScript and ECMAScript.”
I ended up having to wade through a sea of ambiguous and seemingly conflicting results:
“ECMAScript is a standard.”
“JavaScript is a standard.”
“ECMAScript is a specification.”
“JavaScript is an implementation of the ECMAScript standard.”
“ECMAScript is standardized JavaScript.”
“ECMAScript is a language.”
“JavaScript is a dialect of ECMAScript.”
“ECMAScript is JavaScript.”

Holding back the urge to cry, I bucked up and decided to commit to some painful yet productive research.
This article represents my current understanding of the differences between JavaScript and ECMAScript. It is geared towards people who are familiar with JavaScript but would like a clearer understanding of its relationship with ECMAScript, web browsers, Babel, and more. You will also learn about scripting languages, JavaScript engines, and JavaScript runtimes for good measure.
So get pumped.
A JavaScript/ECMAScript glossary
Below is a list of definitions, designed with a focus on consistency and clarity. The definitions are not 100% complete. They are constructed in a way that provides a high-level understanding of the connection and relationship between JavaScript and ECMAScript.
Without further ado, let’s get started.
Ecma International
An organization that creates standards for technologies.

To illustrate an example of “standard” (though not one created by Ecma), think of all the keyboards you have ever used. Did the vast majority have letters in the same order, and a space bar, an Enter key, arrow keys, with numbers displayed in a row at the top? This is because most keyboard manufacturers base their keyboard design on the QWERTY layout standard.
ECMA-262
This is a standard published by Ecma International. It contains the specification for a general purpose scripting language.

ECMA-262 is a standard like QWERTY, but instead of representing a keyboard layout specification, it represents a scripting language specification called ECMAScript.
Think of ECMA-262 as ECMAScript’s reference number.

ECMA-260, ECMA-261, ECMA-262. There’s ECMAScript.
A scripting language
A programming language designed specifically for acting on an existing entity or system
For a general idea of what makes a programming language a scripting language, consider the commands “walk”, “run”, and “jump.” These actions require something to carry them out, perhaps a person, a dog, or a video game character. Without an actor to perform these commands, “walk”, “run”, and “jump” wouldn’t make sense. This set of actions is analogous to a scripting language that focuses on manipulating an external entity.
ECMAScript
The specification defined in ECMA-262 for creating a general purpose scripting language.
Synonym: ECMAScript specification

Photo credit: code.tutsplus.com
While ECMA-262 is the name of the standard, it represents the scripting language specification ECMAScript.
ECMAScript provides the rules, details, and guidelines that a scripting language must observe to be considered ECMAScript compliant.

An excerpt from the ECMAScript 2017 Language Specification. The document is only about 900 pages, if you are looking for a light read.
JavaScript
A general purpose scripting language that conforms to the ECMAScript specification.
It is a dialect of the ECMAScript language.

Photo credit: Udemy
JavaScript is the coffee-flavored language with which I love to program. ECMAScript is the specification it’s based on. By reading the ECMAScript specification, you learn how to create a scripting language. By reading the JavaScript documentation, you learn how to use a scripting language.
When people call JavaScript a “dialect of the ECMAScript language,” they mean it in the same sense as when they refer to dialect to describe languages like English, French, or Chinese. A dialect derives most of its lexicon and syntax from its parent language, but deviates enough to deserve distinction.
JavaScript mostly implements the ECMAScript specification as described in ECMA-262, but a handful of differences do exist. Mozilla outlines JavaScript’s non-ECMAScript language features here:

A screenshot from September 3, 2017. It is a list of JavaScript’s experimental features that are not a part of ECMAScript (at least not yet).
A JavaScript engine
A program or interpreter that understands and executes JavaScript code.
Synonyms: JavaScript interpreter, JavaScript implementation

Photo credit: translatemedia.com
JavaScript engines are commonly found in web browsers, including V8 in Chrome, SpiderMonkey in Firefox, and Chakra in Edge. Each engine is like a language module for its application, allowing it to support a certain subset of the JavaScript language.
A JavaScript engine to a browser is like language comprehension to a person. If we re-visit our example of the actions “walk”, “run”, “jump”, a JavaScript engine is the part of an “entity” that actually understands what these actions mean.
This analogy helps to explain a few things about browsers:

Photo credit: datavizcatalogue.com
Differences in browser performance
Two people may recognize the command “jump”, but one may react to the command faster because the person can understand and process the command faster than the other person. Similarly, two browsers can understand JavaScript code, but one runs it faster because its JavaScript engine is implemented more efficiently.

Photo credit: vcsolutions.com
Differences in browser support
Consider the differences that exist between people who speak the same language. Even if many people speak English, some may know some words, expressions, and syntax rules that others don’t, and vice versa. Browsers are the same way. Even though the JavaScript engines of browsers all understand JavaScript, some browsers have a greater understanding of the language than others. There are differences in the way browsers support the language.
With regards to browser support, people usually talk about “ECMAScript compatibility” rather than “JavaScript compatibility,” even though JavaScript engines parse and execute… well, JavaScript. This can be a little confusing, but there is an explanation.

This table is part of a browser support table in the ECMAScript Wikipedia page. JavaScript versions are not mentioned here.
If you will recall, ECMAScript is a specification for what a scripting language could look like. Releasing a new edition of ECMAScript does not mean that all JavaScript engines in existence suddenly have those new features. It is up to the groups or organizations who are responsible for JavaScript engines to be up-to-date about the latest ECMAScript specification, and to adopt its changes.
Therefore, developers tend to ask questions like, “What version of ECMAScript does this browser support?” or “Which ECMAScript features does this browser support?” They want to know if Google, Mozilla, and Microsoft have gotten around to updating their browsers’ JavaScript engines — for example V8, SpiderMonkey, and Chakra, respectively — with the features described in the latest ECMAScript.
The ECMAScript compatibility table is a good resource for answering those questions.
If a new edition of ECMAScript comes out, JavaScript engines do not integrate the entire update at one go. They incorporate the new ECMAScript features incrementally, as seen in this excerpt from Firefox’s JavaScript changelog:

In Firefox 50, pieces of ES2015 and ES2017 were both implemented in Firefox’s JavaScript engine, SpiderMonkey. Other pieces of ES2015 and ES2017 were implemented before, and will continue to be implemented in the future.
A JavaScript runtime
The environment in which the JavaScript code runs and is interpreted by a JavaScript engine.The runtime provides the host objects that JavaScript can operate on and work with.
Synonyms: Host environment

Photo credit: Emuparadise
The JavaScript runtime is the “existing entity or system” mentioned in the scripting language definition. Code passes through the JavaScript engine, and once parsed and understood, an entity or system performs the interpreted actions. A dog walks, a person runs, a video game character jumps (or in the case of the above image, wrecks).
Applications make themselves available to JavaScript scripting by providing “host objects” at runtime. For the client side, the JavaScript runtime would be the web browser, where host objects like windows and HTML documents are made available for manipulation.
Have you ever worked with the window or document host objects? The window and document objects are not actually a part of the core JavaScript language. They are Web APIs, objects provided by a browser acting as JavaScript’s host environment. For the server side, the JavaScript runtime is Node.js. Server-related host objects such as the file system, processes, and requests are provided in Node.js.
An interesting point: different JavaScript runtimes can share the same JavaScript engine. V8, for example, is the JavaScript engine used in both Google Chrome and Node.js — two very different environments.
ECMAScript 6
It is the sixth edition of the ECMA-262 standard, and features major changes and improvements to the ECMAScript specification.
Synonyms: ES6, ES2015, and ECMAScript 2015

This edition of ECMAScript changed its name from ES6 to ES2015 because in 2015 Ecma International decided to switch to annual releases of ECMAScript. Accordingly, Ecma International also started to name new editions of the ECMAScript specification based on the year they are released. In short, ES6 and ES2015 are two different names for the same thing.
Babel
A transpiler that can convert ES6 code to ES5 code.

Photo credit: HTML5Hive.org
Developers can use the shiny new features that come with ES6, but may be concerned with cross-browser compatibility for their web apps. At the time of the writing of this article, Edge and Internet Explorer do not fully support features from the ES6 specification.
Concerned developers can use Babel to convert their codes using ES6, which converts it to a functionally equivalent version of ES5. All of the major browsers fully support ES5 features, so they can run the code without any issues.
One more interesting tidbit
I hope you found this information about JavaScript and ECMAScript useful. Before we wrap up things here, I’d like to share one more piece of information that needs to be clarified for fledgling web developers like me.
Chicken or the egg
A confusing bit of history is that JavaScript was created in 1996. It was then submitted to Ecma International in 1997 for standardization, which resulted in ECMAScript. At the same time, because JavaScript conformed to the ECMAScript specification, JavaScript is an example of an ECMAScript implementation.
That leaves us with this fun fact: ECMAScript is based on JavaScript, and JavaScript is based on ECMAScript.
I know.
It sounds exactly like the time-travel trope of people being their own parent — a little wonky, but kind of fun to think about.

Tagged , ,

我忍不住要留言