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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
22+高頻實(shí)用的JavaScript片段(2020年)

廢話不多話,在本文中,列出了一些比較常用或者實(shí)用的的 JavaScript 代碼片段,希望對(duì)你們有所幫助。

公司主營(yíng)業(yè)務(wù):網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出茂名免費(fèi)做網(wǎng)站回饋大家。

1.三元運(yùn)算符

 
 
 
 
  1. let someThingTrue = true 
  2. if(someThingTrue){ 
  3.     handleTrue() 
  4. }else{ 
  5.     handleFalse() 
  6.  
  7. ****** 以下是簡(jiǎn)短版本 ****** 
  8.  
  9. let someThingTrue = true 
  10. someThingTrue ?  handleTrue() : handleFalse() 

2.短路或運(yùn)算

 
 
 
 
  1. const defaultValue = "SomeDefaultValue" 
  2. let someValueNotSureOfItsExistance = null 
  3. let expectingSomeValue = someValueNotSureOfItsExistance ||     defaultValue 
  4.  
  5. console.log(expectingSomeValue) // SomeDefaultValue 

3. 條件成立

 
 
 
 
  1. let someValue = true 
  2. if (someValue) { 
  3.   console.log('條件成立!') 

4. for 循環(huán)

 
 
 
 
  1. for (let i = 0; i < 1e2; i++) { // 代替 i<100 是不是有點(diǎn)酷 
  2. let someValues = [1, 2, 4] 
  3. for (let val in someValues) { 
  4.   console.log(val) 
  5. let obj = { 
  6.   'key1': 'value1', 
  7.   'key2': 'value2', 
  8.   'key3': 'value3' 
  9. for (let key in obj) { 
  10.   console.log(key) 

5. 值到對(duì)象的映射

 
 
 
 
  1. let x='x',y='y' 
  2. let obj = {x,y} 
  3.  
  4. console.log(obj) // {x: "x", y: "y"} 

6. Object.entries()

 
 
 
 
  1. const credits = { 
  2.   producer: '大遷世界', 
  3.   name: '前端小智', 
  4.   rating: 9 
  5. const arr = Object.entries(credits) 
  6. console.log(arr) 
  7.  
  8. *** 輸出 *** 
  9. [ [ 'producer', '大遷世界' ], [ 'name', '前端小智' ], [ 'rating', 9 ] ] 

7. Object.values()

 
 
 
 
  1. const credits = { 
  2.   producer: '大遷世界', 
  3.   name: '前端小智', 
  4.   rating: 9 
  5. const arr = Object.values(credits) 
  6. console.log(arr) 
  7.  
  8. *** 輸出 *** 
  9.  
  10. [ '大遷世界', '前端小智', 9 ] 

8. 模板字面量

 
 
 
 
  1. let name = '前端小智' 
  2. let age = 20 
  3. var someStringConcatenateSomeVariable = `我是 ${name},今年 ${age} 歲` 
  4. console.log(someStringConcatenateSomeVariable) 

9. 解構(gòu)賦值

 
 
 
 
  1. import { observable, action, runInAction } from 'mobx'; 

10.多行字符串

 
 
 
 
  1. let multiLineString = `some string\n 
  2. with multi-line of\n 
  3. characters\n` 
  4.  
  5. console.log(multiLineString) 

11.Array.find 簡(jiǎn)寫

 
 
 
 
  1. const pets = [{ 
  2.     type: 'Dog', 
  3.     name: 'Max' 
  4.   }, 
  5.   { 
  6.     type: 'Cat', 
  7.     name: 'Karl' 
  8.   }, 
  9.   { 
  10.     type: 'Dog', 
  11.     name: 'Tommy' 
  12.   } 
  13. pet = pets.find(pet => pet.type === 'Dog' && pet.name === 'Tommy') 
  14.  
  15. console.log(pet) // { type: 'Dog', name: 'Tommy' } 

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

早期的做法

 
 
 
 
  1. function area(h, w) { 
  2.   if (!h) { 
  3.     h = 1; 
  4.   } 
  5.   if (!w) { 
  6.     w = 1; 
  7.   } 
  8.   return h * w 

ES6 以后的做法

 
 
 
 
  1. function area(h = 1, w = 1) { 
  2.   return h * w 

13.箭頭函數(shù)的簡(jiǎn)寫

 
 
 
 
  1. let sayHello = (name) => { 
  2.   return `你好,${name}` 
  3.  
  4. console.log(sayHello('前端小智')) 

簡(jiǎn)寫如下:

 
 
 
 
  1. let sayHello = name => `你好,${name}` 
  2.  
  3. console.log(sayHello('前端小智')) 

14.隱式返回

 
 
 
 
  1. let someFuncThatReturnSomeValue = (value) => { 
  2.   return value + value 
  3. console.log( 
  4. someFuncThatReturnSomeValue('前端小智')) 

簡(jiǎn)寫如下:

 
 
 
 
  1. let someFuncThatReturnSomeValue = (value) => ( 
  2.   value + value 
  3. console.log(someFuncThatReturnSomeValue('前端小智')) 

15.函數(shù)必須有參數(shù)值

 
 
 
 
  1. function mustHavePatamMethod(param) { 
  2.   if (param === undefined) { 
  3.     throw new Error('Hey You must Put some param!'); 
  4.   } 
  5.   return param; 

以像這樣重寫:

 
 
 
 
  1. mustHaveCheck = () => { 
  2.   throw new Error('Missing parameter!') 
  3. methodShoudHaveParam = (param = mustHaveCheck()) => { 
  4.   return param 

16.charAt() 簡(jiǎn)寫

 
 
 
 
  1. 'SampleString'.charAt(0) // S 
  2. // 簡(jiǎn)寫 
  3. 'SampleString'[0] 

17.有條件的函數(shù)調(diào)用

 
 
 
 
  1. function fn1() { 
  2.   console.log('I am Function 1') 
  3.  
  4. function fn2() { 
  5.   console.log('I am Function 2') 
  6. /* 
  7. 長(zhǎng)的寫法 
  8. */ 
  9. let checkValue = 3; 
  10. if (checkValue === 3) { 
  11.   fn1() 
  12. } else { 
  13.   fn2() 

簡(jiǎn)短的寫法:

 
 
 
 
  1. (checkValue === 3 ? fn1 : fn2)() 

17.Math.Floor 簡(jiǎn)寫

 
 
 
 
  1. let val = '123.95' 
  2.  
  3. console.log(Math.floor(val)) // 常規(guī)寫法 
  4. console.log(~~val) // 簡(jiǎn)寫 

18.Math.pow  簡(jiǎn)寫

 
 
 
 
  1. Math.pow(2, 3) // 8 
  2. // 簡(jiǎn)寫 
  3. 2 ** 3 // 8 

19.將字符串轉(zhuǎn)換為數(shù)字

 
 
 
 
  1. const num1 = parseInt('100') 
  2. // 簡(jiǎn)寫 
  3. console.log(+"100") 
  4. console.log(+"100.2") 

20.&& 運(yùn)算

 
 
 
 
  1. let value = 1; 
  2. if (value === 1) 
  3.   console.log('Value is one') 
  4. //OR In short  
  5. value && console.log('Value is one') 

21.toString 簡(jiǎn)寫

 
 
 
 
  1. let someNumber = 123 
  2. console.log(someNumber.toString()) // "123" 
  3. // 簡(jiǎn)寫 
  4. console.log(`${someNumber}`) // "123" 

22.可選的鏈運(yùn)算符(即將發(fā)布)

現(xiàn)在有一個(gè)關(guān)于ECMAScript的新提議,值得了解。

 
 
 
 
  1. let someUser = { 
  2.   name: 'Jack' 
  3. let zip = someUser?.address?.zip //可選鏈接,像 Swift  

如果 zip是undefined ,則不會(huì)引發(fā)錯(cuò)誤。

該語法還支持函數(shù)和構(gòu)造函數(shù)調(diào)用

 
 
 
 
  1. let address = getAddressByZip.?(12345) 

如果getAddressByZip是調(diào)用它的函數(shù),否則,表達(dá)式將以u(píng)ndefined的形式計(jì)算。

23. 使用對(duì)象的方式來替換 switch 語法

 
 
 
 
  1. let fruit = 'banana'; 
  2. let drink; 
  3. switch (fruit) { 
  4.   case 'banana': 
  5.     drink = 'banana juice'; 
  6.     break; 
  7.   case 'papaya': 
  8.     drink = 'papaya juice'; 
  9.     break; 
  10.   default: 
  11.     drink = 'Unknown juice!' 
  12. console.log(drink) // banana juice 

作者:xor 譯者:前端小智 來源:medium

原文:https://medium.com/javascript-in-plain-english/some-js-shortcuts-82bc2f56146e

本文轉(zhuǎn)載自微信公眾號(hào)「 大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系 大遷世界公眾號(hào)。


網(wǎng)站題目:22+高頻實(shí)用的JavaScript片段(2020年)
本文路徑:http://m.5511xx.com/article/coegoee.html