
由于 Babel,WebPack, React 技术的流行, 小明同学已经开始在前端代码里面用上了 ECMAScript 2015 (以下称 ES6 ) 的众多特性。import, export, class, arrow function, let const 等等关键词甚至都已经开始变成小明的肌肉记忆。
但是等等,对于小明这样既写 React 又写 Node.js 的的人来说,又有点分裂了。经常性的在 Node.js 里面写 import xxx from 'xxx'; 然后被 Node.js 的解析器无情的拒绝;
另外对于一致性有点追求的人肯定受不了同一个仓库, 同一类脚本, 同一个人维护却有两种风格。

恩, 不能忍!
Node.js 对 ES6 的支持
其实 Node.js 对 ES6 的很多特性都已经开始支持了。 在 Node.js 使用的 JS 引擎 V8 里面将不同状态 ES6 特性分成了 3 个等级:
shipping (已经分发并默认开启的特性)
staged (已经分发, 但需要使用 –harmony 参数开启的特性)
in progress (开发中, 还不稳定或者未实现的特性, 不推荐使用)
『shipping』 已经开启的 ES6 特性
本文使用的 Node.js 版本号:
稳定版本:4.2.4
对应 V8 引擎的版本:4.5.103.35
最新版本:5.3.0
对应 V8 引擎版本:4.6.85.31
目前默认开启的 ES6 特性如下:
Block scoping
let (strict mode only)
const
function-in-blocks (strict mode only [1])
Classes (strict mode only)
Collections
Map
WeakMap
Set
WeakSet
Typed arrays
Generators
Binary and Octal literals
Object literal extensions (shorthand properties and methods)
Promises
New String methods
Symbols
Template strings
Arrow Functions
new.target [2]
Object.assign
Spread operator [2]
对于这些官方引擎的特性,由于在底层实现,在性能与稳定性都有更高保障,在任何时候我们都应该优先使用。
『staged』 需要使用 --harmony 参数开启的 ES6 特性
Symbol.toStringTag
Array.prototype.includes (可以直接使用 polyfill 支持, 5.x 版本)
Rest Parameters (可以直接用 transform-es2015-parameters 转换,支持更全面, 5.x 版本)
『in progress』 开发中的 ES 特性
--harmony_modules (enable "harmony modules") |
『in progress』特性是那些将要支持(但具体时间未知)的特性。
开启模块的 ES6 模式
Babel 是一个插件式的 JavaScript 编译器, 能将一些当前 JS 引擎中不支持的特性和语法, 通过一个个特定插件,转换成当前引擎可以理解的 JS 脚本。 我们可以使用 Babel 来转换我们的 Node.js 脚本。
接下来, 我们就可以去 Babel 插件列表去选择对应的转换插件来为我们的 Node.js 插上隐形的翅膀了。
首先, 我们需要确认我们需要 Babel 添加哪些特性支持。
选择 ES 转换的原则
优先使用原生特性
优先选择那些稳定实现的特性。 由于一些 ES 特性需要引擎的底层支持才能完美支持, 通过代码转换可能很难完美支持, 对于这种特性只能不用或少用
基于这个原则, 小明筛选出如下插件。
transform-strict-mode (由于很多 ES 特性需要 严格模式才能打开, 添加这个插件就会自动在所有文件上添加
'use strict';)transform-es2015-modules-commonjs (将 ES6 模块标准 转换成 Node.js 用的 CMD 模块标准)
transform-es2015-spread (支持 ES6 的 spread 操作符)
transform-es2015-destructuring (支持 赋值解构)
transform-es2015-parameters (支持默认参数, 参数解构, 以及其他参数)
转换示例: import
from
import Mod from './mod'; |
to
; |
转换示例: export
from:
export default class Mod { |
to:
; |
上面这些选择的插件可以根据个人口味以及 Node.js 版本 进行添加或删除。选好模块, 我们就可以安装插件以及创建对应得babel配置文件去处理
-
intellij,idea,git for windows一直占用,内存过高
fatal: Unable to create,git/index.lock': File exists.
-
consola 教程
consola 和 console 只差一个字母,并且它们都是控制器日志输出的好帮手。console 在某些方面,使用有些局限性。consola 是一个功能更丰富,更漂亮的控制台日志输出控件。今天我们一起来学习它的
-
AndroidStudio 多层级 Module 对 aar 引用问题
有个arr文件被放到Module A中引用,现在Module B又依赖了Module A,则在编译过程中会发生错误,Module B找不到aar文件。
-
nginx支持socket
安装nginx,stream模块默认不安装的,需要手动添加参数:–with-stream,根据自己系统版本选择nginx1.9或以上版本。
-
Java中List转换为数组,数组转List
List转换为Array可以这样处理,反过来,如果要将数组转成List怎么办呢
-
git合并时冲突<<<<<<< HEAD
head 到 =======里面的lalala是自己的commit的内容 =========到 >>>>>>里面的hehehe是下拉的内容
-
Java中数组怎么深度复制
有时候循环进行一些操作,放入list,发现,list中的数据都是一个数据,这就尴尬了,我们需要深度复制,才能解决这个问题。或者生成新的,也就是深度复制。
-
static inner class和非static inner class的实例化问题(can only instantiate non-static inner class...)
non-static inner class 需要持有外部类实例的引用,所以无法直接调用构造器创建,需要先构造外部类实例,然后通过外部类实例调用构造方法创建,如下:
-
spring post jackson的反序列化需要无参构造函数
JSON parse error: Cannot construct instance of `com.**` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.**` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 2]] ———————————————— 版权声明:本文为CSDN博主「冰夏之夜影」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u011561335/article/details/91346777
-
spring boot爬坑之旅途--shiro-MyShiroConfig配置(config)
spring boot爬坑之旅途--shiro-MyShiroConfig配置(config)
-
intellij,idea,git for windows一直占用,内存过高
fatal: Unable to create,git/index.lock': File exists.
-
consola 教程
consola 和 console 只差一个字母,并且它们都是控制器日志输出的好帮手。console 在某些方面,使用有些局限性。consola 是一个功能更丰富,更漂亮的控制台日志输出控件。今天我们一起来学习它的
-
AndroidStudio 多层级 Module 对 aar 引用问题
有个arr文件被放到Module A中引用,现在Module B又依赖了Module A,则在编译过程中会发生错误,Module B找不到aar文件。
-
nginx支持socket
安装nginx,stream模块默认不安装的,需要手动添加参数:–with-stream,根据自己系统版本选择nginx1.9或以上版本。
-
Java中List转换为数组,数组转List
List转换为Array可以这样处理,反过来,如果要将数组转成List怎么办呢
-
git合并时冲突<<<<<<< HEAD
head 到 =======里面的lalala是自己的commit的内容 =========到 >>>>>>里面的hehehe是下拉的内容
-
Java中数组怎么深度复制
有时候循环进行一些操作,放入list,发现,list中的数据都是一个数据,这就尴尬了,我们需要深度复制,才能解决这个问题。或者生成新的,也就是深度复制。
-
static inner class和非static inner class的实例化问题(can only instantiate non-static inner class...)
non-static inner class 需要持有外部类实例的引用,所以无法直接调用构造器创建,需要先构造外部类实例,然后通过外部类实例调用构造方法创建,如下:
-
spring post jackson的反序列化需要无参构造函数
JSON parse error: Cannot construct instance of `com.**` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.**` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 2]] ———————————————— 版权声明:本文为CSDN博主「冰夏之夜影」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u011561335/article/details/91346777
-
spring boot爬坑之旅途--shiro-MyShiroConfig配置(config)
spring boot爬坑之旅途--shiro-MyShiroConfig配置(config)