Recently reading the "Explore Es6", I think it is important to do a little note, to facilitate future study.
1. Ways to obtain more ES6 resources
There are two groups of ES6 resources:
- "ECMAScript 6 Tools", author Addy Osmani.
- "ECMAScript 6 learning!", author Eric Douglas.
2, non-versioned upgrade
In principle, a new version of the language is an opportunity to clean up outdated features or to change the way features work. This means that the new code will not work in the old implementation of the language, and the old code will not work in the new implementation. Each piece of code is associated with a specific language version. It is common to write two different types of code for two different language versions.
Through backwards compatibility. That means we have to give up some of our ambitions, like cleaning up JavaScript: We can't introduce disruptive changes. Backwards compatibility means that you do not remove the feature or modify it. The principle is: "Do not destroy the Web."
However, we can add new features and make existing features more powerful.
3, Strict mode and ECMAScript 6
ECMAScript 5 introduces strict mode to clean up the language, in the first line of the file or function to put the following content can be turned on strict mode:‘use strict‘;
Strict mode introduces three destructive changes:
Syntax change: Some of the previously valid grammars are not allowed under strict mode. For example:
- Prohibit
with statements. It allows the user to add any object-to-variable scope chain, which slows down the execution of the program and makes it difficult to point out where a variable is pointing.
- It is not allowed to delete a separate identifier (a variable, not a property).
- Functions can only be declared at the top level of the scope.
- More Reserved words: Implements interface let package private protected public static yield.
More kinds of errors. For example:
- Assigning a value to an undeclared variable is thrown
ReferenceError . In non-strict mode, doing so creates a global variable.
- Modifying a read-only property, such as the length property of a string, is thrown
TypeError . In non-strict mode, no effect is produced.
Different semantics: In strict mode, some grammatical structures behave differently. For example:
argumentsNo longer changes as the current parameter value changes.
- In a function that is not a method
this undefined . In non-strict mode, it points to the global object ( window ), which means that if a constructor is called without using it new , some global variables are created.
Strict mode is a good indication that versioning is tricky: even if you can make a clean version of JavaScript, it's hard to be accepted. The main reason is that some existing code is destroyed, the execution speed is reduced, and it is cumbersome to add to the file (let alone the command line of the interaction).
4. Try ECMAScript 6
There are three simple ways to play ES6:
- 1. Web browser: Using the online Babel REPL, an interactive tool that can compile ES6 into ES5. If you choose this way, you don't have to install anything.
- 2. Command line: Use
babel-node , an executable version of node. JS, which can run the ES6 code (internally compiled into ES5). It can be installed via NPM.
- 3. Each JavaScript Engine: Refer to Kangax's ES6 compatible table, which can find out which ES6 features are natively supported in an engine.
5. New values and Math features
You can now write binary and octal numeric literals:
// Es5:hexadecimal 255 // es6:binary 3 // Es6:octal 8
Some new properties are added to the global object number, especially:
-
Number.EPSILON used to compare floating-point numbers and to tolerate rounding errors.
- A method and constant used to determine whether JavaScript integers are safe (no loss of precision within a signed 53-bit range).
parseInt()There is no special support for octal or binary literals!
If you want to parse this literal, you should use it Number() ;
Alternatively, you can remove the prefix and use it parseInt() to pass in the appropriate radix parameters;
//**************************> parseint ('0b111')0> parseint ('0b111',2)0> parseint ('111',2)7> parseint ('0o10')0> parseint ('0o10',8)0> parseint ('Ten',8)8//**************************> Number ('0b111')7> Number ('0o10')8//**************************> parseint ('111',2)7> parseint ('Ten',8)8
Four numeric-related global functions have been added Number isFinite below, these methods are: and isNaN , parseFloat parseInt and. All methods behave like the corresponding global functions, but isFinite isNaN the parameters that do not require them are numbers.
Math.sign(x) Returns x the symbol, 1 or + 1. If x it is NaN or 0, the X is returned
ES6 Study Notes 1