今天用jstransform编写ES6

news/2024/7/19 15:14:09 标签: java, python, javascript, js, php

So there's this recent open-source project from Facebook called jstransform. It's also used by ReactJS. It lets you explore ES6 features and not only explore, but use them in production code.

因此,Facebook最近有一个名为jstransform的开源项目。 ReactJS也使用它。 它使您可以浏览ES6功能,不仅可以浏览生产代码,还可以在生产代码中使用它们。

All you need to do is add the transformation to your static resource pipeline. (Of course you have one, right, for minification and so on)

您需要做的就是将转换添加到静态资源管道中。 (当然,您有一个,对,用于缩小等等)

I took an example from the readme of the project, added all the available transformations and ended up with a simple little script (available on github).

我从项目的自述文件中举了一个例子,添加了所有可用的转换,最后得到了一个简单的小脚本(可在github上找到)。

As a background information, the different transforms are available in the visitors/ directory, they are task-specific, e.g. one adds support for classes, one for fat-arrow functions and so on. I simply included them all. The result is a small command-line utility you can add to your process. How?

作为背景信息, visitors/目录中提供了不同的转换,这些转换是特定于任务的,例如,一个添加了对类的支持,一个添加了对粗箭头功能的支持,等等。 我只是将它们全部包括在内。 结果是可以将一个小的命令行实用程序添加到您的进程中。 怎么样?

安装jstransform (Install jstransform)

$ npm install jstransform

获取我的小es6r脚本 (Get my little es6r script)

$ curl https://raw.githubusercontent.com/stoyan/etc/master/es6r/es6r.js > aaa.js

It's pretty simple:

很简单:

var jstransform = require('jstransform');
var fs = require('fs');
 
var visitors = [];
[
  require('jstransform/visitors/es6-arrow-function-visitors'),
  require('jstransform/visitors/es6-class-visitors'),
  require('jstransform/visitors/es6-object-short-notation-visitors'),
  require('jstransform/visitors/es6-rest-param-visitors'),
  require('jstransform/visitors/es6-template-visitors')
].forEach(function(visitor) {
  visitors = visitors.concat(visitor.visitorList);
});
 
var es6maybe = fs.readFileSync(process.argv[2], 'utf8');
var es5 = jstransform.transform(visitors, es6maybe);
 
console.log(es5.code);

开始转型 (Start transforming)

There are a few examples to get you started

有一些例子可以帮助您入门

E.g.

例如

箭头功能(Arrow functions)

(MDN)

( MDN )

var empty = () => {};
 
var a = [
  "We're up all night 'til the sun",
  "We're up all night to get some",
  "We're up all night for good fun",
  "We're up all night to get lucky"
];
 
var a3 = a.map( s => s.length );

Transforming...

转型中...

$ node es6r.js examples/arrow.js

The result:

结果:

var empty = function()  {};
 
var a = [
  "We're up all night 'til the sun",
  "We're up all night to get some",
  "We're up all night for good fun",
  "We're up all night to get lucky"
];
 
var a3 = a.map( function(s)  {return s.length;} );

班级 (Classes)

class Monkey {
  constructor(name) {
    this.name = name;
  }
  see() {
    return 'see';
  }
  do() {
    return 'doo';
  }
}
 
var monkey = new Monkey('Monkey');
 
console.log([
  monkey.name,
  monkey.see(),
  '-',
  monkey.name,
  monkey.do(),
].join(' '));

After transformation...

改造后...

 
  function Monkey(name) {"use strict";
    this.name = name;
  }
  Monkey.prototype.see=function() {"use strict";
    return 'see';
  };
  Monkey.prototype.do=function() {"use strict";
    return 'doo';
  };
 
 
var monkey = new Monkey('Monkey');
 
console.log([
  monkey.name,
  monkey.see(),
  '-',
  monkey.name,
  monkey.do(),
].join(' '));

As you can see the transformation tries to protect line numbers and identation.

如您所见,转换尝试保护行号和标识。

谢谢! (Thanks!)

There are more examples to explore and you can start getting a taste of ES6 today. You may or may not like it, but at least you can decide by writing code, not just based on gut feeling 🙂

有更多示例可供探索,您今天就可以开始尝试ES6。 您可能喜欢或不喜欢它,但至少您可以通过编写代码来决定,而不仅仅是基于直觉。

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/writing-es6-today-with-jstransform/


http://www.niftyadmin.cn/n/1306917.html

相关文章

Java中的换行符

Java中的换行符 PrintWriter out response.getWriter(); out.write("\r\n"); Java中的换行符“\r\n”

Python多版本管理工具--pyenv

我们在平时的项目开发或者学习中,有可能使用不同的Python版本,大家都知道Python的版本非常多,如果我们把需要的不同版本的Python都下载到服务器上,管理起来会非常困难,多版本并存又容易互相干扰,搞不好整个…

Feliz Navidad圣诞快乐2009

Disregarding my dear wifes opinion that the best present to my blog readers would be to *not* publish my new recording.... Ill just go ahead and do it: 无视我亲爱的妻子的观点,对我的博客读者来说,最好的礼物是*不*发布我的新唱片...。我将继…

Servlet配置

1、在Servlet中&#xff0c;Servlet的配置 WebServlet(name"csvExportServlet",urlPatterns"/servlet/CsvExportServlet") 2、在web.xml中&#xff0c;Servlet的配置 <servlet><description></description><display-name></dis…

YSlow发展:入门

Since version 2.0, YSlow is no longer just a tool, its a platform. You can create your own rules (performance or otherwise), combine them into rulesets, tweak scores to your liking and so on. 从2.0版开始&#xff0c;YSlow不再只是一个工具&#xff0c;而是一个…

修改windows7中文件的权限

1、修改ntkrnlpa.exe的权限 2、鼠标右键&#xff0c;选择“属性” 3、单击“安全”选项&#xff0c;选择“高级” 4、在高级安全设置中&#xff0c;选择“所有者”&#xff0c;修改“所有者”&#xff0c;单击“编辑” 5、选择“第一个” 6、这时&#xff0c;会有window安全提示…

create-react-app + sw-precache = PWA

(this industry and its lingo...) (这个行业及其术语...) PWA - Progressive Web App, its... a long story, go read here PWA-渐进式Web应用&#xff0c;这是一个很长的故事&#xff0c;请在此处阅读React - library for building UIs. Theres a book on React by yours tr…

document.forms[].submit()

document.forms[exportServlet].submit(); &#xff08;1&#xff09;document.forms&#xff1a;表示获取当前页面的所有表单 &#xff08;2&#xff09;document.forms[0]&#xff1a;表示获取当前页面的第一个表单 &#xff08;3&#xff09;document.forms[exportServlet…