ES6 基础

This commit is contained in:
罗祥
2019-12-15 19:03:36 +08:00
parent f0f3336641
commit 0b506230fe
24 changed files with 1858 additions and 11 deletions

View File

@ -0,0 +1,24 @@
function request(url, timeout = 2000, callback = function() {}) {
console.log(`url: ${url}, timeout: ${timeout}`);
callback();
}
request("GitHub"); // url: GitHub, timeout: 2000
request("GitHub", undefined); // url: GitHub, timeout: 2000
request("GitHub", null); // url: GitHub, timeout: null
request("GitHub", 5000); // url: GitHub, timeout: 5000
request("GitHub", 5000, () => console.log("超时异常")); // url: GitHub, timeout: 5000
// 超时异常
function test01(first, second = first) {
console.log(`first: ${first}, second: ${second}`);
}
function test02(first = second, second) {
console.log(`first: ${first}, second: ${second}`);
}
test01(1,undefined);
test01(1,2);
test02(undefined,2);
test02(1,2);

View File

@ -0,0 +1,7 @@
function each(...elements) {
elements.forEach((element) => console.log(element));
}
let list = [1, 2, 3, 4, 5];
each(list);
each(...list);

View File

@ -0,0 +1,34 @@
let doNothing01 = () => {
};
// 等效箭头表达式:
let doNothing02 = function () {
};
let reflect01 = value => value;
// 等效箭头表达式:
let reflect02 = function (value) {
return value;
};
let sum01 = (num1, num2) => num1 + num2;
// 等效箭头表达式:
let sum02 = function (num1, num2) {
return num1 + num2;
};
let getDetail01 = id => ({id: id, name: "heibaiying"});
// 等效箭头表达式:
let getDetail02 = function (id) {
return {
id: id,
name: "heibaiying"
};
};
function test() {
return () => arguments[0]; // 箭头函数能访问包含它的函数的 arguments
}
let arrowFunction = test("hello");
console.log(arrowFunction()); // hello