JavaScript ES6新特性
ES6 主要是为了解决 ES5 的先天不足,比如 JavaScript之前的版本里并没有类 的概念
## 1. 函数参数默认值
function add(a, b, c = 10) {
return a + b + c;
}
let result = add(1, 2, 3);
console.log(result);
let result1 = add(1, 2);
console.log(result1);
function connect(options) {
let host = options.host;
let username = options.username;
}
connect({
host: "localhost",
username: "root",
password: "root",
port: 3306,
});
function connect({ host, username, password, port = 3306 }) {
console.log(host);
console.log(username);
console.log(password);
console.log(port);
}
connect({
host: "localhost",
username: "root",
password: "root",
});
2. 简化对象写法
let name = "奉先";
let change = function () {
console.log("我们可以改变你!!!");
};
const SCHOOL = {
name,
change,
improve() {
console.log("方法也更加简洁了,之前的是improve:function(){}");
},
};
change();
SCHOOL.improve();
console.log(SCHOOL);
3. 箭头函数(Lambda表达式)
function getName() {
console.log(this.name);
}
let getName2 = () => {
console.log(this.name);
};
window.name = "奉先";
const SCHOOL = {
name: "yuanchen",
};
getName();
getName2();
getName.call(SCHOOL);
getName2.call(SCHOOL);
let Person = function (name, age) {
this.name = name;
this.age = age;
};
let me = new Person("xxn", "19");
console.log(me);
let Person = (name, age) => {
this.name = name;
this.age = age;
};
let me = new Person("xxn", "19");
console.log(me);
let fn = function () {
console.log(arguments);
};
fn(1, 2, 3);
let fn = () => {
console.log(arguments);
};
fn(1, 2, 3);
let add = (n) => {
return n + n;
};
let pow = (n) => n * n;
console.log(pow(9));
4. 解构赋值
const F4 = ["小沈阳", "刘能", "赵四", "宋小宝"];
let [xiao, liu, zhao, song] = F4;
console.log(xiao);
console.log(liu);
console.log(zhao);
console.log(song);
const zhao = {
name: "赵本山",
age: "20",
xiaopin: function () {
console.log("我可以演小品");
},
};
let { name, age, xiaopin } = zhao;
console.log(name);
zhao.name = "赵四";
console.log(name);
console.log(age);
console.log(xiaopin);
xiaopin();
zhao.xiaopin();
zhao.xiaopin();
zhao.xiaopin();
zhao.xiaopin();
let { xiaopin } = zhao;
xiaopin();
xiaopin();
xiaopin();
xiaopin();
5. 拓展运算符 …
const tfboys = ["易烊千玺", "王源", "王俊凯"];
function chunwan() {
console.log(arguments);
}
let app = {
url: "miniProgram/sendCode",
method: "POST",
data: {
phone: 17670459756,
},
};
chunwan(tfboys);
chunwan(...tfboys);
const KUAIZI = ["肖央", "王太利"];
const FH = ["曾毅", "玲花"];
const zuixuanxiaopingguo = [...KUAIZI, ...FH];
console.log(zuixuanxiaopingguo);
const SZH = ["E", "G", "M"];
const SYC = [...SZH];
console.log(SYC);
const DIVS = document.querySelectorAll("div");
console.log(DIVS);
const DIVSARR = [...DIVS];
console.log(DIVSARR);
6.模板字符串
var a = 1;
var b = 2;
var sum = a + b;
var res = `a+b的和是${sum}`;
console.log(res);
var obj = { name: "张三;", age: 18 };
var str = `姓名:${obj.name}年龄:${obj.age}`;
console.log(str);
let lovest = "`魏翔`";
let out = lovest + "是我心目中最搞笑的演员";
let lovest2 = `魏翔`;
let out2 = `${lovest2}是我心目中最搞笑的演员`;
console.log(out2);
7. forof遍历数组
for (let x in arr) {
console.log(x);
console.log(typeof x);
console.log(arr[x]);
}
arr.forEach((value, index) => {
console.log(value);
console.log(index);
console.log(typeof index);
});
for (let x of arr) {
console.log(x);
}
8. promise&generator
8.1 all
all的主要作用是等到 [] 括号里的所有promise请求都请求完成后才执行回调函数,常用来在页面刚加载时确保数据全部能刷新出来
<!DOCTYPE html>
<html>
<head>
<title>all</title>
</head>
<body></body>
<script>
var promise1 = new Promise(function (resolve) {
setTimeout(function () {
resolve(1);
alert(1 + "先到");
}, 3000);
});
var promise2 = new Promise(function (resolve) {
setTimeout(function () {
resolve(2);
alert(2 + "先到");
}, 1000);
});
Promise.all([promise1, promise2]).then(function (value) {
alert(value);
});
</script>
</html>
8.2 race
race和all恰恰相反,当 [ ] 括号里的任意promise请求完成后就会执行回调函数里的内容
<!DOCTYPE html>
<html>
<head>
<title>异步</title>
</head>
<body></body>
<script>
function timerPromise(delay) {
return new Promise(function (resolve) {
resolve(delay);
});
}
Promise.race([
timerPromise(1),
timerPromise(32),
timerPromise(64),
timerPromise(128),
]).then(function (value) {
console.log(value);
});
function getURL(URL) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', URL, true);
req.onload = function () {
if (req.status === 200) {
resolve(req.responseText);
} else {
reject(new Error(req.statusText));
}
};
req.onerror = function () {
reject(new Error(req.statusText));
};
req.send();
});
}
var URL = "...";
getURL(URL).then(function onFulfilled(value) {
console.log(value);
});
</script>
</html>
|