i,unbug

Undeniable[Better:见贤思齐,闻庸自省. Unique:独立思索,志在远方. Genuine:谦逊真诚,宽以待人.]

来看看EcmaScript6新特性的类继承和模块化

Posted by on Jun 4, 2012 in JavaScript | 0 comments

ES6新增了很多非常实用的功能,我对其中的类和模块化非常感兴趣,
1.类新增extends继承,super和prototype:

class Point extends Base {
    constructor(x,y) {
       super();
       this[px] = x, this = y;
       this.r = function() { return Math.sqrt(x*x + y*y); }
    }
get x() { return this[px]; }
get y() { return this; }
proto_r() { return Math.sqrt(this[px] * this[px] +
this * this); }
equals(p) { return this[px] === p[px] &&
this === p; }
}

2.模块化其实跟commonjs规范差不多,可以export对外公布方法,也可以import引入模块中的方法.

modules:

module math {
  export function sum(x, y) {
    return x + y;
  }
  export var pi = 3.141593;
}

import {sum, pi} from math;
alert(sum(pi,pi));

详情

Read More