army8735 / jsdc Goto Github PK
View Code? Open in Web Editor NEWJavascript Downcast (es6 to es5)
Home Page: http://army8735.me/jsdc
Javascript Downcast (es6 to es5)
Home Page: http://army8735.me/jsdc
a(...b) -> a.apply(this, [].concat(b))
b可能不是个array,使用iterator
new a().b(...c)
需要解决
像babel提供了两个命令行:
babel
用于编译
babel-node
用于直接基于node执行
jsdc有jsdc-cli提供了,再搞个jsdc-node?
var [a,b] = [1,2];
return {
a,
b
}
等名转换
new A(...this.xxx)
new (Function.prototype.bind.apply(A, [null].concat(function(){ this.xxx }()));
this指向错误,变成window了,需要在new之前保存当前context,替换掉this。
class A {
constructor() {
console.log(new.target.name)
}
}
class B extends A {}
new A // -> A
new B // -> B
babel对应的issue:babel/babel#1088
spec:http://www.ecma-international.org/ecma-262/6.0/index.html#sec-meta-properties
new Cls(...args);
在 traceur 中是展开为:
new (Function.prototype.bind.apply(Cls, $traceurRuntime.spread([null], args)))();
Cannot redefine property
class的同名属性进行set和get方法设置后,因转换为两次同名的Object.defineProperty操作报错。
先用一个临时变量存储之,最后进行统一get、set。
js性能在语法解析树上过慢,主要是递归性能。考虑再提供一个java版本。
其他几个transpiler也是保持行数一致,但是我个人觉得6to5的做法更好。
因为生成代码可读性对于支持调试其实也很重要。但是强制源码行数一致必定导致生成代码可读性的实际下降。而且仅行数对应,也不能解决行较长没有col位置的问题。
现在浏览器都支持sourcemap,所以建议放弃行数对应,改为支持sourcemap,并给出更好的代码排版,甚至可加入注释。比如在注释中包含源码(这样还能解决不支持sourcemap的老浏览器的调试问题)。
现有草案规范尚不完善,等待之。
for(var a of fn()){}
for(var {x} of obj){...}
for({x, y} of obj){...}
class Test {
method() {
super() // <- should throw syntax error here
}
}
It should also apply to normal functions or generators.
See HasDirectSuper
static semantics in the draft.
node中自动判别环境是否支持,不支持则编译
function *hello() {
yield 'hello';
yield 'world';
}
var test = hello();
console.log(test.next().value)
console.log(test.next().value)
console.log(test.next().value)
第三次应该是啥?undefined还是hello?
windows 1.8.1前者,mac后者。
@hax 麻烦解答下规范了……
比较麻烦
关联#12
See https://github.com/mathiasbynens/regexpu. You can use this as a dependency for your transpiler to add support for the ES6 /u
flag for regular expressions.
(Traceur and 6to5 depend on regexpu as well, FWIW.)
LL(1)还是太慢了
处理of、let和generator中的of、let需变更
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-iteration-statements
homunculus 0.7.6
以下按实现逻辑顺序排列
将var申明迁移至最近的作用域起始处
function() {
if(true) {
var a = 1;
}
}
function() {var a;
if(true) {
a = 1;
}
}
必要时将{}替换为function作用域
{
let a = 1;
}
(function() {
let a = 1;
}).call(this);
将let替换为var
let a = 1;
var a = 1;
将const替换为var
const a = 1;
var a = 1;
原型继承,class语法糖仅作简单转换,虽然尚未定案和现实标准实现,但是这是比较稳定的即将实现的特例
class Test() {
constructor(a) {
this.a = a;
}
method() {
}
get data() {
return this.a;
}
}
function Test(a) {
this.a = a;
this.data = a;
}
Test.prototype.method = function() {
}
使用寄生组合式继承
class Father() {
constructor(a) {
this.a = a;
}
method() {
return this.a;
}
}
class Child() {
constructor(a) {
super(a);
}
method() {
return super.method();
}
}
function Father(a) {
this.a = a;
}
Father.prototype.method = function() {
return this.a;
}
function Child(a) {
Father.call(this, a);
}
Child.prototype.method = function() {
return Father.prototype.method.call(this);
}
//这行以下为可阅读性多行写,本应附加在上面行末尾
;(function() {
var prototype = Object.create(Father.prototype);
prototype.constructor = Father;
Child.prototype = prototype;
)();
根据是否全等undefined赋值,默认参数后不能出现非默认参数
function method(a, b = 1) {
}
function method(a, b) {if(typeof b == "undefined") b = 1;
}
将可变参数通过arguments转换为数组
function method(a, ...args) {
}
function method(a, args) {args = Array.prototype.slice.call(arguments, 1);
}
等待es6正式规范或者现实标准实现
module {
exports = {};
}
`template`
function test() {
var fn = (a, b, c) => console.log(this, arguments, a, b, c);
fn.call({name: "inner"}, 4, 5, 6);
}
test.call({name: "outer"}, 1, 2, 3);
// 实际应该输出
// {name: "outer"} [1, 2, 3] 4 5 6
// 你这个实现的输出
// {name: "inner"} [4, 5, 6] 4 5 6
对比实现:
参考链接:
var fn = (...arg) => console.log(arg);
上面代码应该正常解析.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.