文档

esnext

更新时间:

SJS 支持部分 ES6 语法。

let & const

  1. function test(){
  2. let a = 5;
  3. if (true) {
  4. let b = 6;
  5. }
  6. console.log(a); // 5
  7. console.log(b); // 引用错误:b 未定义
  8. }

箭头函数

  1. const a = [1,2,3];
  2. const double = x => x * 2; // 箭头函数
  3. console.log(a.map(double));
  4. var bob = {
  5. _name: "Bob",
  6. _friends: [],
  7. printFriends() {
  8. this._friends.forEach(f =>
  9. console.log(this._name + " knows " + f));
  10. }
  11. };
  12. console.log(bob.printFriends());

更简洁的对象字面量(enhanced object literal)

  1. var handler = 1;
  2. var obj = {
  3. handler, // 对象属性
  4. toString() { // 对象方法
  5. return "string";
  6. },
  7. };
说明:不支持super关键字,不能在对象方法中使用 super

模板字符串(template string)

  1. const h = 'hello';
  2. const msg = `${h} alipay`;

解构赋值(Destructuring)

  1. // array 解构赋值
  2. var [a, ,b] = [1,2,3];
  3. a === 1;
  4. b === 3;
  5. // 对象解构赋值
  6. var { op: a, lhs: { op: b }, rhs: c }
  7. = getASTNode();
  8. // 对象解构赋值简写
  9. var {op, lhs, rhs} = getASTNode();
  10. // 函数参数解构赋值
  11. function g({name: x}) {
  12. console.log(x);
  13. }
  14. g({name: 5});
  15. // 解构赋值默认值
  16. var [a = 1] = [];
  17. a === 1;
  18. // 函数参数:解构赋值 + 默认值
  19. function r({x, y, w = 10, h = 10}) {
  20. return x + y + w + h;
  21. }
  22. r({x:1, y:2}) === 23;

Default + Rest + Spread

  1. // 函数参数默认值
  2. function f(x, y=12) {
  3. // 如果不给y传值,或者传值为 undefied,则 y 的值为 12
  4. return x + y;
  5. }
  6. f(3) == 15;
  7. function f(x, ...y) {
  8. // y 是一个数组
  9. return x * y.length;
  10. }
  11. f(3, "hello", true) == 6;
  12. function f(x, y, z) {
  13. return x + y + z;
  14. }
  15. f(...[1,2,3]) == 6; // 数组解构
  16. const [a, ...b] = [1,2,3]; // 数组解构赋值, b = [2, 3]
  17. const {c, ...other} = {c: 1, d: 2, e: 3}; // 对象解构赋值, other = {d: 2, e: 3}
  18. const d = {...other}; // 对象解构
  • 本页导读 (0)
文档反馈