es學習

過去幾年像 Underscore 和 lodash 等庫進入許多JavaScript程序員的工具函數中。雖然這些工具庫能夠使你的代碼寫起來更容易,可是他們不必定使代碼更簡單或更容易理解。
 
JavaScript不斷髮展,新ES2015和ES2016版本(之前分別稱爲ES6和ES7)包了一堆新功能特性,並很容易使用它們。這些特性使得工具庫之前的一些基本功能已通過時。
因此你可能再也不須要Underscore。
 
例子:
這些例子說明,ES5.1,ES2015和ES2016作這些事情很容易,你可能不須要一個實用程序庫了。ES5已經獲得了全部現代瀏覽器和node.js的支持,要是想支持傳統瀏覽器(好比IE8),還須要像es-shim這樣的幫助腳本。
Arrays(數組)
Iterate(迭代)
.each(array,iteratee)
array.forEach(iteratee)
 
Map
_map(array,iteratee)
 
array.map(iteratee)
 
Find
 
_.find(array,predicate)
 
array.find(predicate)
 
Get a property from each element in an array(萃取數組對象中某屬性值
 
_.pluck(array,propertyName)
 
array.map(value=>value[propertyName])
 
Check if array includes an element(檢查數組中是否包含某個元素)
_.contains(array,element)
 
array.includes(element)
 
Convert an array-like object to array(把一個類數組轉換成一個數組)
_.toArray(arguments)
 
Array.from(arguments)
Create a copy of an array with all falsy values removed.(返回一個除去全部false值的 array副本)
 
_.compact(array)
 
array.filter(x=>!!x)
Create a copy of an array with duplicates removed(返回 array去重後的副本)
 
_.uniq(array)
 
...new Set(array)
Find the index of a value in an array(查找某個值在 array 中的索引值)
_.indexOf(array, value)
 
array.indexOf(value)
 
Create an array with n numbers, starting from x(建立一個 N個數字數組,從x開始)
Underscore
_.range(x, x + n)
 
ES2015
Array.from({ length: n }, (v, k) => k + x)
 
 
Objects(對象)
Names of own enumerable properties(枚舉自身的屬性名)
Underscore
_.keys(object)
 
ES5.1
Object.keys(object)
 
Names of all enumerable properties(枚舉全部的屬性名,包括繼承過來的)
Underscore
_.allKeys(object)
 
ES2015
Reflect.enumerate(object)  // 返回一個迭代器
相關文章
相關標籤/搜索