codecamp

百度智能小程序 esnext

SJS 支持部分 ES6 语法。

let & const

代码示例 

在开发者工具中打开
// demo.sjs
function foo(){
    let str = 'hello sjs';
    if (true) {
        let count = 2;
    }
    console.log(str); // hello sjs
    console.log(count); // 引用错误:count 未定义
}

箭头函数

代码示例 

在开发者工具中打开
// demo.sjs
const arr = [1, 2, 3];
const double = x => x * 2;
console.log(arr.map(double)); // output: [2, 4, 6]

var obj = {
    birth: 1970,
    getAge() {
        const b = this.birth;
        const fn = () => new Date().getFullYear() - this.birth;
        return fn();
    }
};
obj.getAge();

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

代码示例 

在开发者工具中打开
var num = 1;
var obj = {
    num, // 对象属性
    printNum() { // 对象方法
    console.log(num);
    }
};
obj.printNum(); // 1

注: 不支持super关键字,不能在对象方法中使用super

模板字符串(template string)

const NAME = 'sjs';
const msg = `hello ${NAME}`; // hello sjs

解构赋值(Destructuring)

代码示例 

在开发者工具中打开
// array 解构赋值
var [a, ,b] = [1, 2, 3];
a === 1; // true
b === 3; // true

// 对象解构赋值
let { foo , bar } = { foo: 'aaa', bar: 'bbb' };
// foo = 'aaa'
// bar = 'bbb'

// 函数参数解构赋值
// 1.参数是一组有次序的值
function f1([x, y, z]) {
    console.log(x); // 1
    console.log(y); // 2
    console.log(z); // 3
}
f1([1, 2, 3]);

// 2.参数是一组无次序的值
function f2({x, y, z}) {
    console.log(x); // 1
    console.log(y); // 2
    console.log(z); // 3
}
f2({z: 3, y: 2, x: 1});

// 解构赋值默认值
var [a = 1] = [];
a === 1; // true

// 函数参数:解构赋值 + 默认值
function r({a, b, c = 3, d = 4}) {
    return a + b + c + d;
}
r({a: 1, b: 2}) === 10; // true

Default + Rest + Spread

代码示例 

在开发者工具中打开
// 1. Default
function f1(x, y = 2) {
    // 如果不给y传值,或者传值为undefied,则y的值为12
    return x + y;
}
f1(1) === 3; // true

// 2. Rest
function f2(x, ...arr) {
    return x * arr.length;
}
f2(3, 'hello', 'sjs') === 6; // true

// 3. Spread
function f3(x, y, z) {
    return x + y + z;
}
f3(...[1,2,3]) == 6; // 数组解构

// 4. Rest + Spread
const [a, ...b] = [1, 2, 3]; // 数组解构赋值, b = [2, 3]
const {c, ...other} = {c: 1, d: 2, e: 3}; // 对象解构赋值, other = {d: 2, e: 3}
const f = {...other}; // 对象解构 f = {d: 2, e: 3}


百度智能小程序 基础类库
百度智能小程序 Filter过滤器
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }