偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

19個JavaScript有用的簡寫技術(shù)

開發(fā) 前端
本文總結(jié)了19個JavaScript有用的簡寫技術(shù),其中包括三元操作符、短路求值簡寫方式、聲明變量簡寫方法等等,希望對大家有所幫助。

19個JavaScript有用的簡寫技術(shù)

1.三元操作符

當(dāng)想寫if...else語句時,使用三元操作符來代替。

 

  1. const x = 20; 
  2.  
  3. let answer; 
  4.  
  5. if (x > 10) { 
  6.  
  7.     answer = 'is greater'
  8.  
  9. else { 
  10.  
  11.     answer = 'is lesser'
  12.  

簡寫:

 

  1. const answer = x > 10 ? 'is greater' : 'is lesser'

也可以嵌套if語句:

 

  1. const big = x > 10 ? " greater 10" : x 

2.短路求值簡寫方式

當(dāng)給一個變量分配另一個值時,想確定源始值不是null,undefined或空值??梢詫懽珜懸粋€多重條件的if語句。

 

  1. if (variable1 !== null || variable1 !== undefined || variable1 !== '') { 
  2.  
  3.      let variable2 = variable1; 
  4.  

或者可以使用短路求值方法:

 

  1. const variable2 = variable1 || 'new'

3.聲明變量簡寫方法

 

  1. let x; 
  2.  
  3. let y; 
  4.  
  5. let z = 3; 

簡寫方法:

 

  1. let x, y, z=3; 

4.if存在條件簡寫方法

 

  1. if (likeJavaScript === true

簡寫:

 

  1. if (likeJavaScript) 

只有l(wèi)ikeJavaScript是真值時,二者語句才相等

如果判斷值不是真值,則可以這樣:

 

  1. let a; 
  2.  
  3. if ( a !== true ) { 
  4.  
  5. // do something... 
  6.  

簡寫:

 

  1. let a; 
  2.  
  3. if ( !a ) { 
  4.  
  5. // do something... 
  6.  

5.JavaScript循環(huán)簡寫方法

  1. for (let i = 0; i < allImgs.length; i++) 

簡寫:

 

  1. for (let index in allImgs) 

也可以使用Array.forEach:

 

  1. function logArrayElements(element, index, array) { 
  2.  
  3.   console.log("a[" + index + "] = " + element); 
  4.  
  5.  
  6. [2, 5, 9].forEach(logArrayElements); 
  7.  
  8. // logs: 
  9.  
  10. // a[0] = 2 
  11.  
  12. // a[1] = 5 
  13.  
  14. // a[2] = 9 

6.短路評價

給一個變量分配的值是通過判斷其值是否為null或undefined,則可以:

 

  1. let dbHost; 
  2.  
  3. if (process.env.DB_HOST) { 
  4.  
  5.   dbHost = process.env.DB_HOST; 
  6.  
  7. else { 
  8.  
  9.   dbHost = 'localhost'
  10.  

簡寫:

 

  1. const dbHost = process.env.DB_HOST || 'localhost'

7.十進(jìn)制指數(shù)

當(dāng)需要寫數(shù)字帶有很多零時(如10000000),可以采用指數(shù)(1e7)來代替這個數(shù)字:

  1. for (let i = 0; i < 10000; i++) {} 

簡寫:

  1. for (let i = 0; i < 1e7; i++) {} 
  2.  
  3. // 下面都是返回true 
  4.  
  5. 1e0 === 1; 
  6.  
  7. 1e1 === 10; 
  8.  
  9. 1e2 === 100; 
  10.  
  11. 1e3 === 1000; 
  12.  
  13. 1e4 === 10000; 
  14.  
  15. 1e5 === 100000; 

8.對象屬性簡寫

如果屬性名與key名相同,則可以采用ES6的方法:

 

  1. const obj = { x:x, y:y }; 

簡寫:

 

  1. const obj = { x, y }; 

9.箭頭函數(shù)簡寫

傳統(tǒng)函數(shù)編寫方法很容易讓人理解和編寫,但是當(dāng)嵌套在另一個函數(shù)中,則這些優(yōu)勢就蕩然無存。

 

  1. function sayHello(name) { 
  2.  
  3.   console.log('Hello'name); 
  4.  
  5.  
  6.  
  7.  
  8. setTimeout(function() { 
  9.  
  10.   console.log('Loaded'
  11.  
  12. }, 2000); 
  13.  
  14.  
  15.  
  16. list.forEach(function(item) { 
  17.  
  18.   console.log(item); 
  19.  
  20. }); 

簡寫:

 

  1. sayHello = name => console.log('Hello'name); 
  2.  
  3. setTimeout(() => console.log('Loaded'), 2000); 
  4.  
  5. list.forEach(item => console.log(item)); 

10.隱式返回值簡寫

經(jīng)常使用return語句來返回函數(shù)最終結(jié)果,一個單獨語句的箭頭函數(shù)能隱式返回其值(函數(shù)必須省略{}為了省略return關(guān)鍵字)

為返回多行語句(例如對象字面表達(dá)式),則需要使用()包圍函數(shù)體。

 

  1. function calcCircumference(diameter) { 
  2.  
  3.   return Math.PI * diameter 
  4.  
  5.  
  6.  
  7.  
  8. var func = function func() { 
  9.  
  10.   return { foo: 1 }; 
  11.  
  12. }; 

簡寫:

 

  1. calcCircumference = diameter => ( 
  2.  
  3.   Math.PI * diameter; 
  4.  
  5.  
  6.  
  7.  
  8. var func = () => ({ foo: 1 }); 

11.默認(rèn)參數(shù)值

為了給函數(shù)中參數(shù)傳遞默認(rèn)值,通常使用if語句來編寫,但是使用ES6定義默認(rèn)值,則會很簡潔:

 

  1. function volume(l, w, h) { 
  2.  
  3.   if (w === undefined) 
  4.  
  5.     w = 3; 
  6.  
  7.   if (h === undefined) 
  8.  
  9.     h = 4; 
  10.  
  11.   return l * w * h; 
  12.  

簡寫:

 

  1. volume = (l, w = 3, h = 4 ) => (l * w * h); 
  2.  
  3. volume(2) //output: 24 

12.模板字符串

傳統(tǒng)的JavaScript語言,輸出模板通常是這樣寫的。

 

  1. const welcome = 'You have logged in as ' + first + ' ' + last + '.' 
  2.  
  3. const db = 'http://' + host + ':' + port + '/' + database

ES6可以使用反引號和${}簡寫:

 

  1. const welcome = `You have logged in as ${first} ${last}`; 
  2.  
  3. const db = `http://${host}:${port}/${database}`; 

13.解構(gòu)賦值簡寫方法

在web框架中,經(jīng)常需要從組件和API之間來回傳遞數(shù)組或?qū)ο笞置嫘问降臄?shù)據(jù),然后需要解構(gòu)它

 

  1. const observable = require('mobx/observable'); 
  2.  
  3. const action = require('mobx/action'); 
  4.  
  5. const runInAction = require('mobx/runInAction'); 
  6.  
  7. const store = this.props.store; 
  8.  
  9. const form = this.props.form; 
  10.  
  11. const loading = this.props.loading; 
  12.  
  13. const errors = this.props.errors; 
  14.  
  15. const entity = this.props.entity; 

簡寫:

 

  1. import { observable, action, runInAction } from 'mobx'
  2.  
  3. const { store, form, loading, errors, entity } = this.props; 

也可以分配變量名:

 

  1. const { store, form, loading, errors, entity:contact } = this.props; 
  2.  
  3. //***一個變量名為contact 

14.多行字符串簡寫

需要輸出多行字符串,需要使用+來拼接:

 

  1. const lorem = 'Lorem ipsum dolor sit amet, consectetur\n\t' 
  2.  
  3.     + 'adipisicing elit, sed do eiusmod tempor incididunt\n\t' 
  4.  
  5.     + 'ut labore et dolore magna aliqua. Ut enim ad minim\n\t' 
  6.  
  7.     + 'veniam, quis nostrud exercitation ullamco laboris\n\t' 
  8.  
  9.     + 'nisi ut aliquip ex ea commodo consequat. Duis aute\n\t' 
  10.  
  11.     + 'irure dolor in reprehenderit in voluptate velit esse.\n\t' 

使用反引號,則可以達(dá)到簡寫作用:

  1. const lorem = `Lorem ipsum dolor sit amet, consectetur 
  2.  
  3.     adipisicing elit, sed do eiusmod tempor incididunt 
  4.  
  5.     ut labore et dolore magna aliqua. Ut enim ad minim 
  6.  
  7.     veniam, quis nostrud exercitation ullamco laboris 
  8.  
  9.     nisi ut aliquip ex ea commodo consequat. Duis aute 
  10.  
  11.     irure dolor in reprehenderit in voluptate velit esse.` 

15.擴展運算符簡寫

擴展運算符有幾種用例讓JavaScript代碼更加有效使用,可以用來代替某個數(shù)組函數(shù)。

 

  1. // joining arrays 
  2.  
  3. const odd = [1, 3, 5]; 
  4.  
  5. const nums = [2 ,4 , 6].concat(odd); 
  6.  
  7.  
  8.  
  9. // cloning arrays 
  10.  
  11. const arr = [1, 2, 3, 4]; 
  12.  
  13. const arr2 = arr.slice() 

簡寫:

 

  1. // joining arrays 
  2.  
  3. const odd = [1, 3, 5 ]; 
  4.  
  5. const nums = [2 ,4 , 6, ...odd]; 
  6.  
  7. console.log(nums); // [ 2, 4, 6, 1, 3, 5 ] 
  8.  
  9.  
  10.  
  11. // cloning arrays 
  12.  
  13. const arr = [1, 2, 3, 4]; 
  14.  
  15. const arr2 = [...arr]; 

不像concat()函數(shù),可以使用擴展運算符來在一個數(shù)組中任意處插入另一個數(shù)組。

 

  1. const odd = [1, 3, 5 ]; 
  2.  
  3. const nums = [2, ...odd, 4 , 6]; 

也可以使用擴展運算符解構(gòu):

 

  1. const { a, b, ...z } = { a: 1, b: 2, c: 3, d: 4 }; 
  2.  
  3. console.log(a) // 1 
  4.  
  5. console.log(b) // 2 
  6.  
  7. console.log(z) // { c: 3, d: 4 } 

16.強制參數(shù)簡寫

JavaScript中如果沒有向函數(shù)參數(shù)傳遞值,則參數(shù)為undefined。為了增強參數(shù)賦值,可以使用if語句來拋出異常,或使用強制參數(shù)簡寫方法。

 

  1. function foo(bar) { 
  2.  
  3.   if(bar === undefined) { 
  4.  
  5.     throw new Error('Missing parameter!'); 
  6.  
  7.   } 
  8.  
  9.   return bar; 
  10.  

簡寫:

 

  1. mandatory = () => { 
  2.  
  3.   throw new Error('Missing parameter!'); 
  4.  
  5.  
  6.  
  7.  
  8. foo = (bar = mandatory()) => { 
  9.  
  10.   return bar; 
  11.  

17.Array.find簡寫

想從數(shù)組中查找某個值,則需要循環(huán)。在ES6中,find()函數(shù)能實現(xiàn)同樣效果。

 

  1. const pets = [ 
  2.  
  3.   { type: 'Dog'name'Max'}, 
  4.  
  5.   { type: 'Cat'name'Karl'}, 
  6.  
  7.   { type: 'Dog'name'Tommy'}, 
  8.  
  9.  
  10.  
  11.  
  12. function findDog(name) { 
  13.  
  14.   for(let i = 0; i<pets.length; ++i) { 
  15.  
  16.     if(pets[i].type === 'Dog' && pets[i].name === name) { 
  17.  
  18.       return pets[i]; 
  19.  
  20.     } 
  21.  
  22.   } 
  23.  

簡寫:

 

  1. pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy'); 
  2.  
  3. console.log(pet); // { type: 'Dog'name'Tommy' } 

18.Object[key]簡寫

考慮一個驗證函數(shù)

 

  1. function validate(values) { 
  2.  
  3.   if(!values.first
  4.  
  5.     return false
  6.  
  7.   if(!values.last
  8.  
  9.     return false
  10.  
  11.   return true
  12.  
  13.  
  14.  
  15.  
  16. console.log(validate({first:'Bruce',last:'Wayne'})); // true 

假設(shè)當(dāng)需要不同域和規(guī)則來驗證,能否編寫一個通用函數(shù)在運行時確認(rèn)?

 

  1. // 對象驗證規(guī)則 
  2.  
  3. const schema = { 
  4.  
  5.   first: { 
  6.  
  7.     required:true 
  8.  
  9.   }, 
  10.  
  11.   last: { 
  12.  
  13.     required:true 
  14.  
  15.   } 
  16.  
  17.  
  18.  
  19.  
  20. // 通用驗證函數(shù) 
  21.  
  22. const validate = (schemavalues) => { 
  23.  
  24.   for(field in schema) { 
  25.  
  26.     if(schema[field].required) { 
  27.  
  28.       if(!values[field]) { 
  29.  
  30.         return false
  31.  
  32.       } 
  33.  
  34.     } 
  35.  
  36.   } 
  37.  
  38.   return true
  39.  
  40.  
  41.  
  42.  
  43.  
  44. console.log(validate(schema, {first:'Bruce'})); // false 
  45.  
  46. console.log(validate(schema, {first:'Bruce',last:'Wayne'})); // true 

現(xiàn)在可以有適用于各種情況的驗證函數(shù),不需要為了每個而編寫自定義驗證函數(shù)了

19.雙重非位運算簡寫

有一個有效用例用于雙重非運算操作符??梢杂脕泶鍹ath.floor(),其優(yōu)勢在于運行更快,可以閱讀此文章了解更多位運算。

 

  1. Math.floor(4.9) === 4 //true 

簡寫:

 

  1. ~~4.9 === 4 //true  

 

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2011-05-16 08:37:56

JavaScript庫

2021-01-31 23:56:49

JavaScript開發(fā)代碼

2022-07-22 10:06:17

JavaScript代碼

2022-12-25 16:03:31

JavaScript技巧

2022-12-22 14:44:06

JavaScript技巧

2021-03-10 07:20:43

優(yōu)化技術(shù)JavaScript

2025-01-07 10:48:08

2025-02-26 12:00:00

JavaScript代碼開發(fā)

2023-07-18 07:56:31

工具reduce業(yè)務(wù)

2023-02-15 16:19:59

JavaScript技巧API

2023-06-28 00:02:40

2020-06-21 13:57:21

JavaScript開發(fā)代碼

2021-04-19 11:30:06

Java開發(fā)程序

2021-04-21 07:53:12

JavaScript單行程序

2023-05-22 15:53:06

JavaScrip代碼素材

2022-05-30 09:44:11

TypeScriptJavaScript技巧

2023-05-30 15:11:16

JavaScrip開發(fā)功能

2023-09-07 16:28:46

JavaScrip

2022-12-19 15:23:51

JavaScrip開發(fā)語言

2023-10-10 16:16:05

JavaScrip開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號