ES6实用的六个知识点
文章目录
1、模板语言
var str = `
var a = 1;
console.log(a)
`;
let userName = "Dean";
console.log( ` I'm ${userName } ` ) ; // I'm Dean
2、var let const
块级作用域:
{
let a = 1;
var b = 2;
}
console.log(a) // a is not defined
console.log(b) // 2
const定义的是常量
(const声明的变量,内存地址不可以改变,但是空间的值可以改)
let a = 1;
a = 2;
const b = 2;
b = 3; // 报错: Assignment to constant variable (常量不能改变)
3、解构赋值
const {
resName,
resPosition,
parkAreaNum,
parkStationNum,
resDesImg
} = resProduct.data;
Object.assign(vm, {
resName,
resPosition,
parkAreaNum,
parkStationNum,
resDesImg
});
传统的写法是: vm.resName = resProduct.data.resName
4、Spread Operator 展开运算符
function sum(...m){
let total = 0;
for(var i of m) {
total += i;
}
console.log(`total: ${total}`)
}
sum(4,5,6,9);
数组
const color = ["red", "yellow"];
const colorful = [...color, "green", "pink"];
console.log(colorful); //[red, yellow, green, pink]
对象
const alp = { fist: "a", second: "b" };
const alphabets = { ...alp, third: "c" };
console.log(alphabets); //{ "fist": "a", "second": "b", "third": "c"
5、promise
function runAsync1() {
var p = new Promise(function(resolve, reject) {
//做一些异步操作
setTimeout(function() {
console.log('异步任务1执行完成');
resolve('数据1');
}, 1000);
});
return p;
}
function runAsync2() {
var p = new Promise(function(resolve, reject) {
//做一些异步操作
setTimeout(function() {
console.log('异步任务2执行完成');
resolve('数据2');
}, 2000);
});
return p;
}
function runAsync3() {
var p = new Promise(function(resolve, reject) {
//做一些异步操作
setTimeout(function() {
console.log('异步任务3执行完成');
resolve('数据3');
}, 2000);
});
return p;
}
runAsync1()
.then(function(data) {
console.log(data);
return runAsync2();
})
.then(function(data) {
console.log(data);
return runAsync3();
})
.then(function(data) {
console.log(data);
});
6、import、export
import util from "../../js/util.js";
export default {
name: 'home',
data () {},
methods: {}
}
(如有不足之处,请多指教)
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论