ES6 基础
This commit is contained in:
24
code/ES6/src/03_funcations/01_default_parameters.js
Normal file
24
code/ES6/src/03_funcations/01_default_parameters.js
Normal 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);
|
7
code/ES6/src/03_funcations/02_rest_parameters.js
Normal file
7
code/ES6/src/03_funcations/02_rest_parameters.js
Normal 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);
|
34
code/ES6/src/03_funcations/03_arrow_functions.js
Normal file
34
code/ES6/src/03_funcations/03_arrow_functions.js
Normal 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
|
Reference in New Issue
Block a user