日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
這個開源項目絕了!30秒就能理解的JavaScript優(yōu)秀代碼

[[394653]]

今天要和大家分享一個項目,里面精心收集了大量有用的JavaScript代碼片段,讓你能夠在極短的時間內(nèi)可以理解使用它們,分為日期、節(jié)點、功能模塊等部分,你可以直接將文件的這些代碼直接導(dǎo)入到你的的文本編輯器(VSCode,Atom,Sublime)。

這個項目在Github上十分受歡迎,目前標(biāo)星 71.3K,累計分支 7.9K(Github地址:https://github.com/30-seconds/30-seconds-of-code)

下面還是一起來看看這個項目里都有哪些代碼段吧:

數(shù)組:arrayMax

返回數(shù)組中的最大值。將Math.max()與擴展運算符 (...) 結(jié)合使用以獲取數(shù)組中的最大值。

 
 
 
 
  1. const arrayMin = arr => Math.min(...arr); 
  2. // arrayMin([10, 1, 5]) -> 1 

瀏覽器:bottomVisible

如果頁的底部可見, 則返回true, 否則為false。使用scrollY、scrollHeight和clientHeight來確定頁面底部是否可見。

 
 
 
 
  1. const bottomVisible = () => 
  2. document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight; 
  3. // bottomVisible() -> true 

日期:getDaysDiffBetweenDates

返回兩個日期之間的差異 (以天為值)。計算Date對象之間的差異 (以天為值)。

 
 
 
 
  1. const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24); 
  2. // getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9 

函數(shù):chainAsync

鏈異步函數(shù),循環(huán)遍歷包含異步事件的函數(shù)數(shù)組, 當(dāng)每個異步事件完成時調(diào)用next。

 
 
 
 
  1. const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }; 
  2. /* 
  3. chainAsync([ 
  4.   next => { console.log('0 seconds'); setTimeout(next, 1000); }, 
  5.   next => { console.log('1 second');  setTimeout(next, 1000); }, 
  6.   next => { console.log('2 seconds'); } 
  7. ]) 
  8. */ 

數(shù)學(xué):arrayAverage

返回數(shù)字?jǐn)?shù)組的平均值。使用Array.reduce()將每個值添加到累加器中, 并以0的值初始化, 除以數(shù)組的length。

 
 
 
 
  1. const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length; 
  2. // arrayAverage([1,2,3]) -> 2 

節(jié)點:JSONToFile

將 JSON 對象寫入文件。使用fs.writeFile()、模板文本和JSON.stringify()將json對象寫入.json文件。

 
 
 
 
  1. const fs = require('fs'); 
  2. const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) 
  3. // JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' 

對象:cleanObj

移除從 JSON 對象指定的屬性之外的任何特性。使用Object.keys()方法可以遍歷給定的 json 對象并刪除在給定數(shù)組中不是included 的鍵。另外, 如果給它一個特殊的鍵 (childIndicator), 它將在里面深入搜索, 并將函數(shù)應(yīng)用于內(nèi)部對象。

 
 
 
 
  1. const cleanObj = (obj, keysToKeep = [], childIndicator) => { 
  2. Object.keys(obj).forEach(key => { 
  3. if (key === childIndicator) { 
  4. cleanObj(obj[key], keysToKeep, childIndicator); 
  5. } else if (!keysToKeep.includes(key)) { 
  6. delete obj[key]; 
  7. }) 
  8. /* 
  9.   const testObj = {a: 1, b: 2, children: {a: 1, b: 2}} 
  10.   cleanObj(testObj, ["a"],"children") 
  11.   console.log(testObj)// { a: 1, children : { a: 1}} 

 

以上舉的這些示例還只是冰山一角,如果你對這個項目感興趣就趕緊馬克起來。

 


網(wǎng)站題目:這個開源項目絕了!30秒就能理解的JavaScript優(yōu)秀代碼
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/djjioih.html