1、对象简化写法
简化前:
var obj = {
name: name,
age: age,
getName: function () {
return this.name;
},
getAge: function () {
return this.age;
}
}
简化后:
let obj = {
//变量名称可以直接作用对象的属性名称
name,
age,
//对象里面的方法可以简写
getName() {
return this.name;
},
//表达式作为属性名或者方法
['get' + 'age']() {
return this.age;
}
}
2、罗列属性名称
Object.keys(obj)
3、将两个对象合并
Object.assign(obj1,obj2)
4、promise基本用法
new Promise((resolve, reject) => {
$ajax({
url: "",
success: (res) => {
resolve(res)
},
error: (err) => {
reject(err)
}
})
}).then((res) => {
console.log('resolve返回结果:' + res)
}, reason => {
console.log('reject返回结果:' + reason)
})
5、promise链式调用
const promiseFn1 = new Promise((resolve, reject) => {
$ajax({
url: "",
success: (res) => {
resolve(res)
},
error: (err) => {
reject(err)
}
})
});
const promiseFn2 = new Promise((resolve, reject) => {
$ajax({
url: "",
success: (res) => {
resolve(res)
},
error: (err) => {
reject(err)
}
})
});
promiseFn1.then(() => {
console.log('promiseFn1 success')
return promiseFn2;
}).then(() => {
console.log('promiseFn2 success')
})
|