Esnext

SJS supports some of the ES6 syntax.

let & const

copy
function test(){
  let a = 5;
  if (true) {
    let b = 6;
  }
  console.log(a); // 5
  console.log(b); // reference error: b undefined
}

Arrow Function

copy
const a = [1,2,3];
const double = x => x * 2; // arrow function
console.log(a.map(double));

var bob = {
  _name: "Bob",
  _friends: [],
  printFriends() {
    this._friends.forEach(f =>
      console.log(this._name + " knows " + f));
  }
};
console.log(bob.printFriends());

More Concise Object Literal

copy
var handler = 1;
var obj = {
  handler, // object attribute
  toString() { // object method
	return "string";
  },
};

Note: The super keyword is not supported. The super cannot be used in object method.

Template String

copy
const h = 'hello';
const msg = `${h} world`;

Destructuring Assignment

copy
// array destructuring assignment
var [a, ,b] = [1,2,3];
a === 1;
b === 3;
// object destructuring assignment
var { op: a, lhs: { op: b }, rhs: c }
       = getASTNode();
// object destructuring assignment logogram
var {op, lhs, rhs} = getASTNode();
// function parameter destructuring assignment
function g({name: x}) {
  console.log(x);
}
g({name: 5});
// destructuring assignment default
var [a = 1] = [];
a === 1;
// function parameter: destructuring assignment + default
function r({x, y, w = 10, h = 10}) {
  return x + y + w + h;
}
r({x:1, y:2}) === 23;

Default + Rest + Spread

copy
// function parameter default
function f(x, y=12) {
  // if no value is transferred to y or the transferred value is undefined, then y value is 12
  return x + y;
}
f(3) == 15;

function f(x, ...y) {
  // y is an array
  return x * y.length;
}
f(3, "hello", true) == 6;
function f(x, y, z) {
  return x + y + z;
}
f(...[1,2,3]) == 6; // array destructuring
const [a, ...b] = [1,2,3]; // array destructuring assignment, b = [2, 3]
const {c, ...other} = {c: 1, d: 2, e: 3}; // object destructuring assignment, other = {d: 2, e: 3}
const d = {...other}; // object destructuring