创新互联百度小程序教程:esnext
- esnext
- let & const
- 箭头函数
- 更简洁的对象字面量(enhanced object literal)
- 模板字符串(template string)
- 解构赋值(Destructuring)
- Default + Rest + Spread
esnext
SJS 支持部分 ES6 语法。

let & const
代码示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
// demo.sjsfunction foo(){let str = 'hello sjs';if (true) {let count = 2;}// hello sjsconsole.log(str);// 引用错误:count 未定义console.log(count);}
箭头函数
代码示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
// demo.sjsconst arr = [1, 2, 3];const double = x => x * 2;// output: [2, 4, 6]console.log(arr.map(double));var obj = {birth: 1970,getAge() {const b = this.birth;const fn = () => new Date().getFullYear() - this.birth;return fn();}};obj.getAge();
更简洁的对象字面量(enhanced object literal)
代码示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
var num = 1;var obj = {// 对象属性num,// 对象方法printNum() {console.log(num);}};// 1obj.printNum();
注:不支持super关键字,不能在对象方法中使用super
模板字符串(template string)
const NAME = 'sjs';// hello sjsconst msg = `hello ${NAME}`;
解构赋值(Destructuring)
代码示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
// array 解构赋值var [a, ,b] = [1, 2, 3];// truea === 1;// trueb === 3;// 对象解构赋值let { foo , bar } = { foo: 'aaa', bar: 'bbb' };// foo = 'aaa'// bar = 'bbb'// 函数参数解构赋值// 1.参数是一组有次序的值function f1([x, y, z]) {// 1console.log(x);// 2console.log(y);// 3console.log(z);}f1([1, 2, 3]);// 2.参数是一组无次序的值function f2({x, y, z}) {// 1console.log(x);// 2console.log(y);// 3console.log(z);}f2({z: 3, y: 2, x: 1});// 解构赋值默认值var [a = 1] = [];// truea === 1;// 函数参数:解构赋值 + 默认值function r({a, b, c = 3, d = 4}) {return a + b + c + d;}// truer({a: 1, b: 2}) === 10;
Default + Rest + Spread
代码示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
// 1. Defaultfunction f1(x, y = 2) {// 如果不给y传值,或者传值为undefied,则y的值为12return x + y;}// truef1(1) === 3;// 2. Restfunction f2(x, ...arr) {return x * arr.length;}// truef2(3, 'hello', 'sjs') === 6;// 3. Spreadfunction f3(x, y, z) {return x + y + z;}// 数组解构f3(...[1,2,3]) == 6;// 4. Rest + Spread// 数组解构赋值, b = [2, 3]const [a, ...b] = [1, 2, 3];// 对象解构赋值, other = {d: 2, e: 3}const {c, ...other} = {c: 1, d: 2, e: 3};// 对象解构 f = {d: 2, e: 3}const f = {...other};
文章题目:创新互联百度小程序教程:esnext
标题链接:http://www.jxjierui.cn/article/cddiojp.html


咨询
建站咨询
