array.includes(3, 1); // find the number 3 starting from array index 1 // true array.includes(5, 4); //false array.includes(1, -1); // find the number 1 starting from the ending of the array going backwards // false array.includes(11, -3); // true
// fetch a user from github fetch('api.github.com/user/AlbertoMontalesi').then( res => { // return the data in json format return res.json(); }).then(res => { // if everything went well, print the data console.log(res); }).catch( err => { // or print the error console.log(err); })
functionwalk(amount) { returnnewPromise((resolve, reject) => { if (amount < 500) { reject ("the value is too small"); } setTimeout(() =>resolve(`you walked for ${amount}ms`),amount); }); }
functionwalk(amount) { returnnewPromise((resolve, reject) => { if (amount < 500) { reject ("the value is too small"); } setTimeout(() =>resolve(`you walked for ${amount}ms`),amount); }); }
// create an async function asyncfunctiongo() { // use the keyword `await` to wait for the response const res = awaitwalk(500); console.log(res); const res2 = awaitwalk(900); console.log(res2); const res3 = awaitwalk(600); console.log(res3); const res4 = awaitwalk(700); console.log(res4); const res5 = awaitwalk(400); console.log(res5); console.log("finished"); }
go();
// you walked for 500ms // you walked for 900ms // you walked for 600ms // you walked for 700ms // uncaught exception: the value is too small
// we use the rest operator to grab everything else left in the object. let { a, b, ...z } = myObj; console.log(a); // 1 console.log(b); // 3 console.log(z); // {c: 5, d: 8}
想要引用正则匹配到的某一部分字符串可以为捕获组编号。每个捕获组的数字都是唯一的,可以对应的数字引用它们,但是这使正则表达式难以阅读和维护。例如 /(\d{4})-(\d{2})-(\d{2})/ 匹配一个日期,但如果不看上下文的代码,就无法确定哪一组对应于月份,哪一组是一天。当然,如果哪一天需要交换日期和月份的顺序,那么对应的组引用也需要更新。现在,可以使用 (?<name>...) 来为捕获组命名,以表示任何标识符名称。重写上述例子:/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u 每一个命名都是唯一且遵循 ECMA 命名规范的。命名的组可以通过匹配结果的 result 属性来访问。对组的数字引用也会被建立,就像未命名的组一样。看下边几个例子:
+
+
1 2 3 4 5 6 7 8 9 10 11 12 13 14
let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u; let result = re.exec('2015-01-02'); // result.groups.year === '2015'; // result.groups.month === '01'; // result.groups.day === '02';
const bigInt = 1n; // small number, but still of BigInt type const num = 1;
num === bigInt; // false -> they aren't strictly equal num == bigInt; // true num >= bigInt; // true -> they can be compared num + bigInt; // error -> they can't operate with one another
// The following import statement is "syntax sugar" (equivalent but sweeter) // for `{ default as cjsSugar }` in the above import statement: import cjsSugar from'cjs';
// The following import statement is "syntax sugar" (equivalent but sweeter) // for `{ default as cjsSugar }` in the above import statement: import cjsSugar from'cjs';
array.includes(3, 1); // find the number 3 starting from array index 1 // true array.includes(5, 4); //false array.includes(1, -1); // find the number 1 starting from the ending of the array going backwards // false array.includes(11, -3); // true
// fetch a user from github fetch('api.github.com/user/AlbertoMontalesi').then( res => { // return the data in json format return res.json(); }).then(res => { // if everything went well, print the data console.log(res); }).catch( err => { // or print the error console.log(err); })
functionwalk(amount) { returnnewPromise((resolve, reject) => { if (amount < 500) { reject ("the value is too small"); } setTimeout(() =>resolve(`you walked for ${amount}ms`),amount); }); }
functionwalk(amount) { returnnewPromise((resolve, reject) => { if (amount < 500) { reject ("the value is too small"); } setTimeout(() =>resolve(`you walked for ${amount}ms`),amount); }); }
// create an async function asyncfunctiongo() { // use the keyword `await` to wait for the response const res = awaitwalk(500); console.log(res); const res2 = awaitwalk(900); console.log(res2); const res3 = awaitwalk(600); console.log(res3); const res4 = awaitwalk(700); console.log(res4); const res5 = awaitwalk(400); console.log(res5); console.log("finished"); }
go();
// you walked for 500ms // you walked for 900ms // you walked for 600ms // you walked for 700ms // uncaught exception: the value is too small
// we use the rest operator to grab everything else left in the object. let { a, b, ...z } = myObj; console.log(a); // 1 console.log(b); // 3 console.log(z); // {c: 5, d: 8}
想要引用正则匹配到的某一部分字符串可以为捕获组编号。每个捕获组的数字都是唯一的,可以对应的数字引用它们,但是这使正则表达式难以阅读和维护。例如 /(\d{4})-(\d{2})-(\d{2})/ 匹配一个日期,但如果不看上下文的代码,就无法确定哪一组对应于月份,哪一组是一天。当然,如果哪一天需要交换日期和月份的顺序,那么对应的组引用也需要更新。现在,可以使用 (?<name>...) 来为捕获组命名,以表示任何标识符名称。重写上述例子:/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u 每一个命名都是唯一且遵循 ECMA 命名规范的。命名的组可以通过匹配结果的 result 属性来访问。对组的数字引用也会被建立,就像未命名的组一样。看下边几个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u; let result = re.exec('2015-01-02'); // result.groups.year === '2015'; // result.groups.month === '01'; // result.groups.day === '02';
const bigInt = 1n; // small number, but still of BigInt type const num = 1;
num === bigInt; // false -> they aren't strictly equal num == bigInt; // true num >= bigInt; // true -> they can be compared num + bigInt; // error -> they can't operate with one another