2021年要了解的34种JavaScript优化技术

开发 前端
开发人员的生活总是在学习新事物,并且跟上变化的难度不应该比现在已经难,我的动机是介绍所有JavaScript最佳实践,例如速记和功能,我们作为前端开发人员必须知道这些使我们的生活在2021年变得更加轻松。

 开发人员的生活总是在学习新事物,并且跟上变化的难度不应该比现在已经难,我的动机是介绍所有JavaScript最佳实践,例如速记和功能,我们作为前端开发人员必须知道这些使我们的生活在2021年变得更加轻松。

 

[[374765]]

 

您可能已经进行了很长时间的JavaScript开发,但是有时您可能没有使用不需要解决或编写一些额外代码即可解决问题的最新功能。这些技术可以帮助您编写干净且优化的JavaScript代码。此外,这些主题可以帮助您为2021年的JavaScript采访做好准备。

在这里,我将提供一个新系列,介绍速记技术,这些速记技术可帮助您编写更干净和优化的JavaScript代码。这是您在2021年必须知道的JavaScript编码的备忘单。

1.如果有多个条件

我们可以在数组中存储多个值,并且可以使用数组include方法。

 

  1. //longhand 
  2. if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { 
  3.     //logic 
  4. //shorthand 
  5. if (['abc''def''ghi''jkl'].includes(x)) { 
  6.    //logic 

 

2.If true … else 简写

当我们具有不包含更大逻辑的if-else条件时,这是一个更大的捷径。我们可以简单地使用三元运算符来实现该速记。

 

  1. // Longhand 
  2. let test: boolean; 
  3. if (x > 100) { 
  4.     test = true
  5. else { 
  6.     test = false
  7. // Shorthand 
  8. let test = (x > 10) ? true : false
  9. //or we can use directly 
  10. let test = x > 10; 
  11. console.log(test); 

 

当我们有嵌套条件时,我们可以采用这种方式。

 

  1. let x = 300, 
  2. test2 = (x > 100) ? 'greater 100' : (x < 50) ? 'less 50' : 'between 50 and 100'
  3. console.log(test2); // "greater than 100" 

 

3.声明变量

当我们要声明两个具有共同值或共同类型的变量时,可以使用此简写形式。

 

  1. //Longhand  
  2. let test1; 
  3. let test2 = 1; 
  4. //Shorthand  
  5. let test1, test2 = 1; 

 

4.空,未定义,空检查

当我们确实创建新变量时,有时我们想检查为其值引用的变量是否为null或未定义。JavaScript确实具有实现这些功能的非常好的捷径。

 

  1. // Longhand 
  2. if (test1 !== null || test1 !== undefined || test1 !== '') { 
  3.     let test2 = test1; 
  4. // Shorthand 
  5. let test2 = test1 || ''

 

5.空值检查和分配默认值

 

  1. let test1 = null
  2.     test2 = test1 || ''
  3. console.log("null check", test2); // output will be "" 

 

6.未定义值检查和分配默认值

 

  1. let test1 = undefined, 
  2.     test2 = test1 || ''
  3. console.log("undefined check", test2); // output will be "" 

 

正常值检查

 

  1. let test1 = 'test', 
  2.     test2 = test1 || ''; 
  3. console.log(test2); // output: 'test' 

 

(奖金:现在我们可以对主题4,5和6使用??运算符)

空位合并运算符

空合并运算符??如果左侧为null或未定义,则返回右侧的值。默认情况下,它将返回左侧的值。

 

  1. const test= null ?? 'default'
  2. console.log(test); 
  3. // expected output"default"const test1 = 0 ?? 2; 
  4. console.log(test1); 
  5. // expected output: 0 

 

7.给多个变量赋值

当我们处理多个变量并希望将不同的值分配给不同的变量时,此速记技术非常有用。

 

  1. //Longhand  
  2. let test1, test2, test3; 
  3. test1 = 1; 
  4. test2 = 2; 
  5. test3 = 3; 
  6. //Shorthand  
  7. let [test1, test2, test3] = [1, 2, 3]; 

8.赋值运算符的简写

我们在编程中处理很多算术运算符。这是将运算符分配给JavaScript变量的有用技术之一。

 

  1. // Longhand 
  2. test1 = test1 + 1; 
  3. test2 = test2 - 1; 
  4. test3 = test3 * 20; 
  5. // Shorthand 
  6. test1++; 
  7. test2--; 
  8. test3 *= 20; 

 

9.如果存在速记

这是我们大家都在使用的常用速记之一,但仍然值得一提。

 

  1. // Longhand 
  2. if (test1 === true
  3.  
  4. // Shorthand 
  5. if (test1) 

 

注意:如果test1有任何值,它将在if循环后进入逻辑,该运算符通常用于null或未定义的检查。

10.多个条件的AND(&&)运算符

如果仅在变量为true的情况下才调用函数,则可以使用&&运算符。

 

  1. //Longhand  
  2. if (test1) { 
  3.  callMethod();  
  4. }  
  5. //Shorthand  
  6. test1 && callMethod(); 

 

11. foreach循环速记

这是迭代的常用速记技术之一。

 

  1. // Longhand 
  2. for (var i = 0; i < testData.length; i++) 
  3.  
  4. // Shorthand 
  5. for (let i in testData) or  for (let i of testData) 

 

每个变量的数组

 

  1. function testData(element, index, array) { 
  2.   console.log('test[' + index + '] = ' + element); 
  3.  
  4. [11, 24, 32].forEach(testData); 
  5. // logs: test[0] = 11, test[1] = 24, test[2] = 32 

 

12.比较返回值

我们也可以在return语句中使用比较。它将避免我们的5行代码,并将它们减少到1行。

 

  1. // Longhand 
  2. let test; 
  3. function checkReturn() { 
  4.     if (!(test === undefined)) { 
  5.         return test; 
  6.     } else { 
  7.         return callMe('test'); 
  8.     } 
  9. var data = checkReturn(); 
  10. console.log(data); //output test 
  11. function callMe(val) { 
  12.     console.log(val); 
  13. // Shorthand 
  14. function checkReturn() { 
  15.     return test || callMe('test'); 

 

13.箭头函数

 

  1. //Longhand  
  2. function add(a, b) {  
  3.    return a + b;  
  4. }  
  5. //Shorthand  
  6. const add = (a, b) => a + b; 

 

更多示例。

 

  1. function callMe(name) { 
  2.   console.log('Hello'name); 
  3. callMe = name => console.log('Hello'name); 

 

14.短函数调用

我们可以使用三元运算符来实现这些功能。

 

  1. // Longhand 
  2. function test1() { 
  3.   console.log('test1'); 
  4. }; 
  5. function test2() { 
  6.   console.log('test2'); 
  7. }; 
  8. var test3 = 1; 
  9. if (test3 == 1) { 
  10.   test1(); 
  11. else { 
  12.   test2(); 
  13. // Shorthand 
  14. (test3 === 1? test1:test2)(); 

 

15.Switch速记

我们可以将条件保存在键值对象中,并可以根据条件使用。

 

  1. // Longhand 
  2. switch (data) { 
  3.   case 1: 
  4.     test1(); 
  5.   break; 
  6.  
  7.   case 2: 
  8.     test2(); 
  9.   break; 
  10.  
  11.   case 3: 
  12.     test(); 
  13.   break; 
  14.   // And so on... 
  15.  
  16. // Shorthand 
  17. var data = { 
  18.   1: test1, 
  19.   2: test2, 
  20.   3: test 
  21. }; 
  22.  
  23. data[something] && data[something](); 

 

16.隐式返回速记

使用箭头功能,我们可以直接返回值,而不必编写return语句。

 

  1. //longhand 
  2. function calculate(diameter) { 
  3.   return Math.PI * diameter 
  4. //shorthand 
  5. calculate = diameter => ( 
  6.   Math.PI * diameter; 

 

17.小数基指数

 

  1. // Longhand 
  2. for (var i = 0; i < 10000; i++) { ... } 
  3.  
  4. // Shorthand 
  5. for (var i = 0; i < 1e4; i++) { 

 

18.默认参数值

 

  1. //Longhand 
  2. function add(test1, test2) { 
  3.   if (test1 === undefined) 
  4.     test1 = 1; 
  5.   if (test2 === undefined) 
  6.     test2 = 2; 
  7.   return test1 + test2; 
  8. //shorthand 
  9. add = (test1 = 1, test2 = 2) => (test1 + test2); 
  10. add() //output: 3 

 

19.点差运算符速记

 

  1. //longhand 
  2. // joining arrays using concat 
  3. const data = [1, 2, 3]; 
  4. const test = [4 ,5 , 6].concat(data); 
  5. //shorthand 
  6. // joining arrays 
  7. const data = [1, 2, 3]; 
  8. const test = [4 ,5 , 6, ...data]; 
  9. console.log(test); // [ 4, 5, 6, 1, 2, 3] 

 

对于克隆,我们也可以使用传播运算符。

 

  1. //longhand 
  2.  
  3. // cloning arrays 
  4. const test1 = [1, 2, 3]; 
  5. const test2 = test1.slice() 
  6. //shorthand 
  7.  
  8. // cloning arrays 
  9. const test1 = [1, 2, 3]; 
  10. const test2 = [...test1]; 

 

20.模板文字

如果您厌倦了在单个字符串中使用+来连接多个变量,那么这种速记方式将消除您的头痛。

 

  1. //longhand 
  2. const welcome = 'Hi ' + test1 + ' ' + test2 + '.' 
  3. //shorthand 
  4. const welcome = `Hi ${test1} ${test2}`; 

 

21.多行字符串速记

当我们在代码中处理多行字符串时,可以使用以下功能:

 

  1. //longhand 
  2. const data = 'abc abc abc abc abc abc\n\t' 
  3.     + 'test test,test test test test\n\t' 
  4. //shorthand 
  5. const data = `abc abc abc abc abc abc 
  6.          test test,test test test test` 

 

22.对象属性分配

 

  1. let test1 = 'a';  
  2. let test2 = 'b'
  3. //Longhand  
  4. let obj = {test1: test1, test2: test2};  
  5. //Shorthand  
  6. let obj = {test1, test2}; 

23.字符串成数字

 

  1. //Longhand  
  2. let test1 = parseInt('123');  
  3. let test2 = parseFloat('12.3');  
  4. //Shorthand  
  5. let test1 = +'123';  
  6. let test2 = +'12.3'

 

24.分配速记

 

  1. //longhand 
  2. const test1 = this.data.test1; 
  3. const test2 = this.data.test2; 
  4. const test2 = this.data.test3; 
  5. //shorthand 
  6. const { test1, test2, test3 } = this.data; 

 

25. Array.find的简写

当我们确实有一个对象数组并且我们想要根据对象属性查找特定对象时,find方法确实很有用。

 

  1. const data = [{ 
  2.         type: 'test1'
  3.         name'abc' 
  4.     }, 
  5.     { 
  6.         type: 'test2'
  7.         name'cde' 
  8.     }, 
  9.     { 
  10.         type: 'test1'
  11.         name'fgh' 
  12.     }, 
  13. function findtest1(name) { 
  14.     for (let i = 0; i < data.length; ++i) { 
  15.         if (data[i].type === 'test1' && data[i].name === name) { 
  16.             return data[i]; 
  17.         } 
  18.     } 
  19. //Shorthand 
  20. filteredData = data.find(data => data.type === 'test1' && data.name === 'fgh'); 
  21. console.log(filteredData); // { type: 'test1'name'fgh' } 

 

26.查找条件速记

如果我们有代码来检查类型,并且基于类型需要调用不同的方法,我们可以选择使用多个else if或进行切换,但是如果我们的速记比这更好呢?

 

  1. // Longhand 
  2. if (type === 'test1') { 
  3.   test1(); 
  4. else if (type === 'test2') { 
  5.   test2(); 
  6. else if (type === 'test3') { 
  7.   test3(); 
  8. else if (type === 'test4') { 
  9.   test4(); 
  10. else { 
  11.   throw new Error('Invalid value ' + type); 
  12. // Shorthand 
  13. var types = { 
  14.   test1: test1, 
  15.   test2: test2, 
  16.   test3: test3, 
  17.   test4: test4 
  18. }; 
  19.   
  20. var func = types[type]; 
  21. (!func) && throw new Error('Invalid value ' + type); func(); 

 

27.速记按位索引

当我们迭代数组以查找特定值时,我们确实使用indexOf()方法,如果我们找到更好的方法呢?让我们看看这个例子。

 

  1. //longhand 
  2. if(arr.indexOf(item) > -1) { // item found  
  3. if(arr.indexOf(item) === -1) { // item not found 
  4. //shorthand 
  5. if(~arr.indexOf(item)) { // item found 
  6. if(!~arr.indexOf(item)) { // item not found 

 

按位(〜)运算符将返回非-1的真实值。取反就像做!〜一样简单。另外,我们也可以使用include()函数:

 

  1. if (arr.includes(item)) {  
  2. // true if the item found 

 

28. Object.entries()

此功能有助于将对象转换为对象数组。

 

  1. const data = { test1: 'abc', test2: 'cde', test3: 'efg' }; 
  2. const arr = Object.entries(data); 
  3. console.log(arr); 
  4. /** Output
  5. [ [ 'test1''abc' ], 
  6.   [ 'test2''cde' ], 
  7.   [ 'test3''efg' ] 
  8. **/ 

 

29. Object.values()

这也是ES8中引入的一项新功能,它执行与Object.entries()类似的功能,但没有关键部分:

 

  1. const data = { test1: 'abc', test2: 'cde' }; 
  2. const arr = Object.values(data); 
  3. console.log(arr); 
  4. /** Output
  5. 'abc''cde'
  6. **/ 

 

30. Double Bitwise简写

(双重NOT按位运算符方法仅适用于32位整数)

 

  1. // Longhand 
  2. Math.floor(1.9) === 1 // true 
  3.  
  4. // Shorthand 
  5. ~~1.9 === 1 // true 

 

31.重复一个字符串多次

要一次又一次地重复相同的字符,我们可以使用for循环并将它们添加到同一循环中,但是如果我们有一个简写方法呢?

 

  1. //longhand  
  2. let test = '';  
  3. for(let i = 0; i < 5; i ++) {  
  4.   test += 'test ';  
  5. }  
  6. console.log(str); // test test test test test  
  7. //shorthand  
  8. 'test '.repeat(5); 

 

32.在数组中查找最大值和最小值

 

  1. const arr = [1, 2, 3];  
  2. Math.max(…arr); // 3 
  3. Math.min(…arr); // 1 

 

33.从字符串中获取字符

 

  1. let str = 'abc'
  2. //Longhand  
  3. str.charAt(2); // c 
  4. //Shorthand  
  5. Note: If we know the index of the array then we can directly use index insted of character.If we are not sure about index it can throw undefined 
  6. str[2]; // c 

 

34.功率速记

数学指数幂函数的简写:

 

  1. //longhand 
  2. Math.pow(2,3); // 8 
  3. //shorthand 
  4. 2**3 // 8 

 

责任编辑:华轩 来源: 今日头条
相关推荐

2021-02-24 11:13:28

网络网络通信互联网

2021-03-10 07:20:43

优化技术JavaScript

2022-02-22 23:39:15

JavaScript编程语言Web

2020-12-29 10:28:01

业务连续性经理数字计划投资

2021-04-01 14:51:15

物联网技术传感器

2018-01-11 15:47:38

2019-11-28 15:37:33

云计算技术工具

2020-04-23 11:03:09

前端语言开发

2020-10-13 06:56:19

JavaScript异常类型开发

2021-06-23 10:39:40

联网汽车车联网物联网

2021-06-07 09:08:00

物联网设备物联网IOT

2021-07-19 13:00:13

智慧城市物联网

2021-06-07 11:33:24

服务器优化TIME-WAIT

2023-06-06 15:31:13

JavaScript开发

2021-01-26 01:03:36

云原生工具云原生

2021-03-31 14:29:19

云计算云迁移

2021-04-01 15:59:47

2022-07-29 09:17:46

JavaScriptReactJS学习

2021-01-07 14:41:37

JavaScript开发技术

2021-04-30 23:43:04

人工智能机器学习算法
点赞
收藏

51CTO技术栈公众号