自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

2021年要了解的34種JavaScript優(yōu)化技術

開發(fā) 前端
開發(fā)人員的生活總是在學習新事物,并且跟上變化的難度不應該比現(xiàn)在已經(jīng)難,我的動機是介紹所有JavaScript最佳實踐,例如速記和功能,我們作為前端開發(fā)人員必須知道這些使我們的生活在2021年變得更加輕松。

 開發(fā)人員的生活總是在學習新事物,并且跟上變化的難度不應該比現(xiàn)在已經(jīng)難,我的動機是介紹所有JavaScript最佳實踐,例如速記和功能,我們作為前端開發(fā)人員必須知道這些使我們的生活在2021年變得更加輕松。

 

[[374765]]

 

您可能已經(jīng)進行了很長時間的JavaScript開發(fā),但是有時您可能沒有使用不需要解決或編寫一些額外代碼即可解決問題的最新功能。這些技術可以幫助您編寫干凈且優(yōu)化的JavaScript代碼。此外,這些主題可以幫助您為2021年的JavaScript采訪做好準備。

在這里,我將提供一個新系列,介紹速記技術,這些速記技術可幫助您編寫更干凈和優(yōu)化的JavaScript代碼。這是您在2021年必須知道的JavaScript編碼的備忘單。

1.如果有多個條件

我們可以在數(shù)組中存儲多個值,并且可以使用數(shù)組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條件時,這是一個更大的捷徑。我們可以簡單地使用三元運算符來實現(xiàn)該速記。

 

  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.空,未定義,空檢查

當我們確實創(chuàng)建新變量時,有時我們想檢查為其值引用的變量是否為null或未定義。JavaScript確實具有實現(xiàn)這些功能的非常好的捷徑。

 

  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' 

 

(獎金:現(xiàn)在我們可以對主題4,5和6使用??運算符)

空位合并運算符

空合并運算符??如果左側(cè)為null或未定義,則返回右側(cè)的值。默認情況下,它將返回左側(cè)的值。

 

  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循環(huán)后進入邏輯,該運算符通常用于null或未定義的檢查。

10.多個條件的AND(&&)運算符

如果僅在變量為true的情況下才調(diào)用函數(shù),則可以使用&&運算符。

 

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

 

11. foreach循環(huán)速記

這是迭代的常用速記技術之一。

 

  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) 

 

每個變量的數(shù)組

 

  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.箭頭函數(shù)

 

  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.短函數(shù)調(diào)用

我們可以使用三元運算符來實現(xiàn)這些功能。

 

  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速記

我們可以將條件保存在鍵值對象中,并可以根據(jù)條件使用。

 

  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.小數(shù)基指數(shù)

 

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

 

18.默認參數(shù)值

 

  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.字符串成數(shù)字

 

  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的簡寫

當我們確實有一個對象數(shù)組并且我們想要根據(jù)對象屬性查找特定對象時,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.查找條件速記

如果我們有代碼來檢查類型,并且基于類型需要調(diào)用不同的方法,我們可以選擇使用多個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.速記按位索引

當我們迭代數(shù)組以查找特定值時,我們確實使用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()函數(shù):

 

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

 

28. Object.entries()

此功能有助于將對象轉(zhuǎn)換為對象數(shù)組。

 

  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中引入的一項新功能,它執(zhí)行與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位整數(shù))

 

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

 

31.重復一個字符串多次

要一次又一次地重復相同的字符,我們可以使用for循環(huán)并將它們添加到同一循環(huán)中,但是如果我們有一個簡寫方法呢?

 

  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.在數(shù)組中查找最大值和最小值

 

  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.功率速記

數(shù)學指數(shù)冪函數(shù)的簡寫:

 

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

 

責任編輯:華軒 來源: 今日頭條
相關推薦

2021-02-24 11:13:28

網(wǎng)絡網(wǎng)絡通信互聯(lián)網(wǎng)

2021-03-10 07:20:43

優(yōu)化技術JavaScript

2022-02-22 23:39:15

JavaScript編程語言Web

2020-12-29 10:28:01

業(yè)務連續(xù)性經(jīng)理數(shù)字計劃投資

2021-04-01 14:51:15

物聯(lián)網(wǎng)技術傳感器

2018-01-11 15:47:38

2025-03-11 07:00:00

數(shù)組JavaScript開發(fā)

2020-04-23 11:03:09

前端語言開發(fā)

2019-11-28 15:37:33

云計算技術工具

2021-06-07 09:08:00

物聯(lián)網(wǎng)設備物聯(lián)網(wǎng)IOT

2021-07-19 13:00:13

智慧城市物聯(lián)網(wǎng)

2021-06-23 10:39:40

聯(lián)網(wǎng)汽車車聯(lián)網(wǎng)物聯(lián)網(wǎng)

2020-10-13 06:56:19

JavaScript異常類型開發(fā)

2021-01-26 01:03:36

云原生工具云原生

2021-04-01 15:59:47

2021-06-07 11:33:24

服務器優(yōu)化TIME-WAIT

2021-03-31 14:29:19

云計算云遷移

2023-06-06 15:31:13

JavaScript開發(fā)

2021-01-07 14:41:37

JavaScript開發(fā)技術

2021-04-30 23:43:04

人工智能機器學習算法
點贊
收藏

51CTO技術棧公眾號