日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
新老手必備的34種JavaScript簡寫優(yōu)化技術

原文:https://javascript.plainenglish.io/34-javascript-optimization-techniques-to-know-in-2021-d561afdf73c3

創(chuàng)新互聯(lián)擁有十年成都網(wǎng)站建設工作經(jīng)驗,為各大企業(yè)提供網(wǎng)站制作、網(wǎng)站設計服務,對于網(wǎng)頁設計、PC網(wǎng)站建設(電腦版網(wǎng)站建設)、成都APP應用開發(fā)、wap網(wǎng)站建設(手機版網(wǎng)站建設)、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、國際域名空間等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設計、網(wǎng)絡營銷經(jīng)驗,集策劃、開發(fā)、設計、營銷、管理等網(wǎng)站化運作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設項目的能力。

作者:Atit

開發(fā)者的生活總是在學習新的東西,跟上變化不應該比現(xiàn)在更難,我的動機是介紹所有JavaScript的最佳實踐,比如簡寫功能,作為一個前端開發(fā)者,我們必須知道,讓我們的生活在2021年變得更輕松。

你可能做了很長時間的JavaScript開發(fā),但有時你可能沒有更新最新的特性,這些特性可以解決你的問題,而不需要做或編寫一些額外的代碼。這些技術可以幫助您編寫干凈和優(yōu)化的JavaScript代碼。此外,這些主題可以幫助你為2021年的JavaScript面試做準備。

1.如果有多個條件

我們可以在數(shù)組中存儲多個值,并且可以使用數(shù)組 include 方法。

 
 
 
 
  1. //Longhand
  2. if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {
  3.   //logic
  4. }
  5. //Shorthand
  6. if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
  7.   //logic
  8. }

2.如果為真…否則簡寫

這對于我們有 if-else 條件,里面不包含更大的邏輯時,是一個較大的捷徑。我們可以簡單的使用三元運算符來實現(xiàn)這個簡寫。

 
 
 
 
  1. // Longhand
  2. let test: boolean;
  3. if (x > 100) {
  4.   test = true;
  5. } else {
  6.   test = false;
  7. }
  8. // Shorthand
  9. let test = (x > 10) ? true : false;
  10. //or we can use directly
  11. let test = x > 10;
  12. 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, Undefined,空檢查

當我們創(chuàng)建新的變量時,有時我們想檢查我們引用的變量的值是否為空或undefined。JavaScript確實有一個非常好的簡寫工具來實現(xiàn)這些功能。

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

5.null值檢查和分配默認值

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

6.undefined值檢查和分配默認值

 
 
 
 
  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'

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) or if (test1 !== "") or if (test1 !== null)
  3. // Shorthand //it will check empty string,null and undefined too
  4. if (test1)

注意:如果test1有任何值,它將在if循環(huán)后進入邏輯,該運算符主要用于 null 或 undefined 的檢查。

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

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

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

11.foreach循環(huán)簡寫

這是迭代的常用簡寫技術之一。

 
 
 
 
  1. // Longhand
  2. for (var i = 0; i < testData.length; i++)
  3. // Shorthand
  4. 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中比較

我們也可以在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. }
  10. var data = checkReturn();
  11. console.log(data); //output test
  12. function callMe(val) {
  13.     console.log(val);
  14. }
  15. // Shorthand
  16. function checkReturn() {
  17.     return test || callMe('test');
  18. }

13.箭頭函數(shù)

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

更多示例。

 
 
 
 
  1. function callMe(name) {
  2.   console.log('Hello', name);
  3. }
  4. 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. }
  14. // Shorthand
  15. (test3 === 1? test1:test2)();

15. Switch簡寫

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

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

16.隱式返回簡寫

使用箭頭函數(shù),我們可以直接返回值,而不必編寫return語句。

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

17.小數(shù)基數(shù)指數(shù)

 
 
 
 
  1. // Longhand
  2. for (var i = 0; i < 10000; i++) { ... }
  3. // Shorthand
  4. 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. }
  9. //shorthand
  10. add = (test1 = 1, test2 = 2) => (test1 + test2);
  11. 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. // cloning arrays
  3. const test1 = [1, 2, 3];
  4. const test2 = test1.slice()
  5. //shorthand
  6. // cloning arrays
  7. const test1 = [1, 2, 3];
  8. 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.   {
  3.     type: 'test1',
  4.     name: 'abc'
  5.   },
  6.   {
  7.     type: 'test2',
  8.     name: 'cde'
  9.   },
  10.   {
  11.     type: 'test1',
  12.     name: 'fgh'
  13.   },
  14. ]
  15. function findtest1(name) {
  16.   for (let i = 0; i < data.length; ++i) {
  17.     if (data[i].type === 'test1' && data[i].name === name) {
  18.       return data[i];
  19.     }
  20.   }
  21. }
  22. //Shorthand
  23. filteredData = data.find(data => data.type === 'test1' && data.name === 'fgh');
  24. console.log(filteredData); // { type: 'test1', name: 'fgh' }

26.查找條件簡寫

如果我們有代碼來檢查類型,根據(jù)類型需要調(diào)用不同的方法,我們可以選擇使用多個else ifs或者switch,但是如果我們有比這更好的簡寫方法呢?

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

27.按位索引簡寫

當我們遍歷數(shù)組以查找特定值時,我們確實使用 indexOf() 方法,如果找到更好的方法該怎么辦?讓我們看看這個例子。

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

按位(?)運算符將返回除-1以外的任何值的真實值。否定它就像做 ~~ 一樣簡單。另外,我們也可以使用 include() 函數(shù):

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

28.Object.entries()

此函數(shù)有助于將對象轉換為對象數(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. ]
  9. **/

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.雙按位簡寫

雙重NOT按位運算符方法僅適用于32位整數(shù))

 
 
 
 
  1. // Longhand
  2. Math.floor(1.9) === 1 // true
  3. // Shorthand
  4. ~~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. console.log(str); // test test test test test 
  6. //shorthand 
  7. '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

本文轉載自微信公眾號「前端全棧開發(fā)者」,可以通過以下二維碼關注。轉載本文請聯(lián)系前端全棧開發(fā)者公眾號。


網(wǎng)頁題目:新老手必備的34種JavaScript簡寫優(yōu)化技術
本文來源:http://www.5511xx.com/article/cocesoj.html