1. 声明变量
//Longhand let x; let y = 20; //Shorthand let x, y = 20;
2. 给多个变量赋值(使用数组解构来在一行中给多个变量赋值)
//Longhand let a, b, c; a = 5; b = 8; c = 12; //Shorthand let [a, b, c] = [5, 8, 12];
3. 三元运算符
//Longhand let marks = 26; let result; if(marks >= 30){ result = ‘Pass‘; }else{ result = ‘Fail‘; } //Shorthand let result = marks >= 30 ? ‘Pass‘ : ‘Fail‘;
4. 赋默认值
//Longhand let imagePath; let path = getImagePath(); if(path !== null && path !== undefined && path !== ‘‘) { imagePath = path; } else { imagePath = ‘default.jpg‘; } //Shorthand let imagePath = getImagePath() || ‘default.jpg‘;
5. 与 (&&) 短路运算
//Longhand if (isLoggedin) { goToHomepage(); } //Shorthand isLoggedin && goToHomepage();
6. 交换两个变量
let x = ‘Hello‘, y = 55; //Longhand const temp = x; x = y; y = temp; //Shorthand [x, y] = [y, x];
7. 箭头函数
//Longhand function add(num1, num2) { return num1 + num2; } //Shorthand const add = (num1, num2) => num1 + num2;
8. 模板字符串
//Longhand console.log(‘You got a missed call from ‘ + number + ‘ at ‘ + time); //Shorthand console.log(`You got a missed call from ${number} at ${time}`);
9. 多行字符串
//Longhand console.log(‘JavaScript, often abbreviated as JS, is a\n‘ + ‘programming language that conforms to the \n‘ + ‘ECMAScript specification. JavaScript is high-level,\n‘ + ‘often just-in-time compiled, and multi-paradigm.‘ ); //Shorthand console.log(`JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.`);
10. 多条件检查
//Longhand if (value === 1 || value === ‘one‘ || value === 2 || value === ‘two‘) { // Execute some code } // Shorthand 1 if ([1, ‘one‘, 2, ‘two‘].indexOf(value) >= 0) { // Execute some code } // Shorthand 2 if ([1, ‘one‘, 2, ‘two‘].includes(value)) { // Execute some code }
11. 对象属性复制
let firstname = ‘Amitav‘; let lastname = ‘Mishra‘; //Longhand let obj = {firstname: firstname, lastname: lastname}; //Shorthand let obj = {firstname, lastname};
12. 字符串转成数字
//Longhand let total = parseInt(‘453‘); let average = parseFloat(‘42.6‘); //Shorthand let total = +‘453‘; let average = +‘42.6‘;
13. 重复一个字符串多次
//Longhand let str = ‘‘; for(let i = 0; i < 5; i ++) { str += ‘Hello ‘; } console.log(str); // Hello Hello Hello Hello Hello // Shorthand ‘Hello ‘.repeat(5);
14. 指数幂
//Longhand const power = Math.pow(4, 3); // 64 // Shorthand const power = 4**3; // 64
15. 双非位运算符 (~~)
//Longhand const floor = Math.floor(6.8); // 6 // Shorthand const floor = ~~6.8; // 6
16. 找出数组中的最大和最小数字
// Shorthand const arr = [2, 8, 15, 4]; Math.max(...arr); // 15 Math.min(...arr); // 2
17. For 循环
let arr = [10, 20, 30, 40]; //Longhand for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } //Shorthand //for of loop for (const val of arr) { console.log(val); } //for in loop for (const index in arr) { console.log(arr[index]); }
18. 合并数组
let arr1 = [20, 30]; //Longhand let arr2 = arr1.concat([60, 80]); // [20, 30, 60, 80] //Shorthand let arr2 = [...arr1, 60, 80]; // [20, 30, 60, 80]
19. 深拷贝多级对象
let obj = {x: 20, y: {z: 30}}; //Longhand const makeDeepClone = (obj) => { let newObject = {}; Object.keys(obj).map(key => { if(typeof obj[key] === ‘object‘){ newObject[key] = makeDeepClone(obj[key]); } else { newObject[key] = obj[key]; } }); return newObject; } const cloneObj = makeDeepClone(obj); //Shorthand const cloneObj = JSON.parse(JSON.stringify(obj)); //Shorthand for single level object let obj = {x: 20, y: ‘hello‘}; const cloneObj = {...obj};
20. 获取字符串中的字符
let str = ‘jscurious.com‘; //Longhand str.charAt(2); // c //Shorthand str[2]; // c
原文:https://www.cnblogs.com/weblf/p/14366337.html