新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)百度小程序教程:esnext
- esnext
- let & const
- 箭頭函數(shù)
- 更簡(jiǎn)潔的對(duì)象字面量(enhanced object literal)
- 模板字符串(template string)
- 解構(gòu)賦值(Destructuring)
- Default + Rest + Spread
esnext
SJS 支持部分 ES6 語法。

let & const
代碼示例
在開發(fā)者工具中打開
在開發(fā)者工具中打開
在 WEB IDE 中打開
// demo.sjsfunction foo(){let str = 'hello sjs';if (true) {let count = 2;}// hello sjsconsole.log(str);// 引用錯(cuò)誤:count 未定義console.log(count);}
箭頭函數(shù)
代碼示例
在開發(fā)者工具中打開
在開發(fā)者工具中打開
在 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();
更簡(jiǎn)潔的對(duì)象字面量(enhanced object literal)
代碼示例
在開發(fā)者工具中打開
在開發(fā)者工具中打開
在 WEB IDE 中打開
var num = 1;var obj = {// 對(duì)象屬性num,// 對(duì)象方法printNum() {console.log(num);}};// 1obj.printNum();
注:不支持super關(guān)鍵字,不能在對(duì)象方法中使用super
模板字符串(template string)
const NAME = 'sjs';// hello sjsconst msg = `hello ${NAME}`;
解構(gòu)賦值(Destructuring)
代碼示例
在開發(fā)者工具中打開
在開發(fā)者工具中打開
在 WEB IDE 中打開
// array 解構(gòu)賦值var [a, ,b] = [1, 2, 3];// truea === 1;// trueb === 3;// 對(duì)象解構(gòu)賦值let { foo , bar } = { foo: 'aaa', bar: 'bbb' };// foo = 'aaa'// bar = 'bbb'// 函數(shù)參數(shù)解構(gòu)賦值// 1.參數(shù)是一組有次序的值function f1([x, y, z]) {// 1console.log(x);// 2console.log(y);// 3console.log(z);}f1([1, 2, 3]);// 2.參數(shù)是一組無次序的值function f2({x, y, z}) {// 1console.log(x);// 2console.log(y);// 3console.log(z);}f2({z: 3, y: 2, x: 1});// 解構(gòu)賦值默認(rèn)值var [a = 1] = [];// truea === 1;// 函數(shù)參數(shù):解構(gòu)賦值 + 默認(rèn)值function r({a, b, c = 3, d = 4}) {return a + b + c + d;}// truer({a: 1, b: 2}) === 10;
Default + Rest + Spread
代碼示例
在開發(fā)者工具中打開
在開發(fā)者工具中打開
在 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;}// 數(shù)組解構(gòu)f3(...[1,2,3]) == 6;// 4. Rest + Spread// 數(shù)組解構(gòu)賦值, b = [2, 3]const [a, ...b] = [1, 2, 3];// 對(duì)象解構(gòu)賦值, other = {d: 2, e: 3}const {c, ...other} = {c: 1, d: 2, e: 3};// 對(duì)象解構(gòu) f = {d: 2, e: 3}const f = {...other};
當(dāng)前文章:創(chuàng)新互聯(lián)百度小程序教程:esnext
文章路徑:http://www.5511xx.com/article/cddiojp.html


咨詢
建站咨詢
