博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
esnext —— 将 ES6 转成 JavaScript 代码
阅读量:6532 次
发布时间:2019-06-24

本文共 2538 字,大约阅读时间需要 8 分钟。

esnext 是一个 JavaScript 库,可以将 ES6 草案规范语法转成今天的 JavaScript 语法。

例如:

/*On the left is code written with new JavaScript features,and on the right is the console output, plus the same codere-written so it can run in today's browsers.Edits made to the code on the left will re-generate andre-run the code on the right. Try it out!*/// Classesclass Person {  constructor(firstName, lastName) {    this.firstName = firstName;    this.lastName = lastName;  }  get name() {    // Template strings    return `${this.firstName} ${this.lastName}`;  }  toString() {    return this.name;  }}console.log(  'Full name is:',  new Person('Michael', 'Bluth'));// Arrow functionsconsole.log([1, 2, 3].map(x => x * x));// Rest paramsfunction join(delim, ...items) {  return items.join(delim);}// Spread argsconsole.log(join('-', ...[415, 555, 1212]));

将被转换成:

/*On the left is code written with new JavaScript features,and on the right is the console output, plus the same codere-written so it can run in today's browsers.Edits made to the code on the left will re-generate andre-run the code on the right. Try it out!*/// Classesvar $__Array$prototype$slice = Array.prototype.slice;var $__Object$defineProperties = Object.defineProperties;var Person = function() {  "use strict";  function Person(firstName, lastName) {    this.firstName = firstName;    this.lastName = lastName;  }  $__Object$defineProperties(Person.prototype, {    name: {      get: function() {        // Template strings        return "" + this.firstName + " " + this.lastName + "";      },      enumerable: true,      configurable: true    },    toString: {      value: function() {        return this.name;      },      enumerable: false,      writable: true    }  });  $__Object$defineProperties(Person, {});  return Person;}();console.log(  'Full name is:',  new Person('Michael', 'Bluth'));// Arrow functionsconsole.log([1, 2, 3].map(function(x) {  return x * x;}));// Rest paramsfunction join(delim) {  var $__arguments = arguments;  var items = [].slice.call($__arguments, 1);  return items.join(delim);}// Spread argsconsole.log(join.apply(null, ['-'].concat($__Array$prototype$slice.call([415, 555, 1212]))));

使用方法:

var transpiler = require('es6-module-transpiler');var Container = transpiler.Container;var FileResolver = transpiler.FileResolver;var BundleFormatter = transpiler.formatters.bundle;var container = new Container({  resolvers: [new FileResolver(['lib/'])],  formatter: new BundleFormatter()});container.getModule('index');container.write('out/mylib.js');

文章转载自 开源中国社区 [

你可能感兴趣的文章
基础算法10:过滤器(Filter)对指定路径不进行过滤
查看>>
Ajax on Rails 2. The Eras of Web Development
查看>>
Jboss问题总结 - 1
查看>>
Lync2013 恢复-整残之后如何重新安装
查看>>
ISA限制用户上网的技巧:ISA2006系列之八
查看>>
成功恢复FAT32误格式化后所有碎片文件(已覆盖的除外)
查看>>
一招一式攻克linux(二)
查看>>
谁说Windows 7 比 XP 不注重用户体验?
查看>>
Android 机顶盒手势、数据分页演示DEMO
查看>>
SEO终极算法(二)
查看>>
技术部门怎么年终考核最合理?
查看>>
SQL Server2008存储结构之非聚集索引
查看>>
开发人员学Linux(4):使用JMeter对网站和数据库进行压力测试
查看>>
Spring Boot整合MyBatis
查看>>
网址导航类的网站为什么会没落
查看>>
windows无法创建快捷方式 请检查磁盘已满
查看>>
jquery处理textarea中的手动换行
查看>>
使用DELPHI编写一个小的控件
查看>>
利用v$enqueue_lock解决ORA-14450的错误
查看>>
Windows系统磁盘分区详解
查看>>