過去幾年像 Underscore 和 lodash 等庫進入許多JavaScript程序員的工具函數中。雖然這些工具庫可以使你的代碼寫起來更容易,但是他們不一定使代碼更簡單或更容易理解。
各種工具函數庫層出不窮,每個工具庫的寫法也各有不同,這樣給閱讀和維護你代碼的人也帶來了一定的困難,以為他必須了解你使用的這個這個工具庫的函數做了什么事情。
JavaScript不斷發展,新ES2015和ES2016版本(以前分別稱為ES6和ES7)包了一堆新功能特性,並很容易使用它們。這些特性使得工具庫以前的一些基本功能已經過時。
所以你可能不再需要Underscore。
例子:
這些例子說明,ES5.1,ES2015和ES2016做這些事情很容易,你可能不需要一個實用程序庫了。ES5已經得到了所有現代瀏覽器和node.js的支持,要是想支持傳統瀏覽器(比如IE8),還需要像es-shim這樣的幫助腳本。
Arrays(數組)
Iterate(迭代)
- Underscore
- _.each(array, iteratee)
- ES5.1
- array.forEach(iteratee)
Map
- Underscore
- _.map(array, iteratee)
- ES5.1
- array.map(iteratee)
Find(查找)
- Underscore
- _.find(array, predicate)
- ES2015
- array.find(predicate)
Get a property from each element in an array(萃取數組對象中某屬性值)
- Underscore(注:pluck也許是map最常使用的用例模型的簡化版本,即萃取數組對象中某屬性值,返回一個數組。)
- _.pluck(array, propertyName)
- ES2015
- array.map(value => value[propertyName])
Check if array includes an element(檢查數組中是否包含某個元素)
- Underscore
- _.contains(array, element)
- ES2016
- array.includes(element)
Convert an array-like object to array(把一個類數組轉換成一個數組)
- Underscore
- _.toArray(arguments)
- ES2015
- Array.from(arguments)
Create a copy of an array with all falsy values removed.(返回一個除去所有false值的 array副本)
- Underscore
- _.compact(array)
- ES2015
- array.filter(x => !!x)
Create a copy of an array with duplicates removed(返回 array去重后的副本)
- Underscore
- _.uniq(array)
- ES2015
- [...new Set(array)]
Find the index of a value in an array(查找某個值在 array 中的索引值)
- Underscore
- _.indexOf(array, value)
- ES5.1
- 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) // 返回一個迭代器
Values(值)
- Underscore
- _.values(object)
- ES5.1
- Object.keys(object).map(key => object[key])
Create a new object with the given prototype(創建具有給定原型的新對象)
- Underscore
- _.create(proto, propertiesObject)
- ES5.1
- Object.create(proto, propertiesObject)
Create a new object from merged properties(創建一個合並屬性后的新對象)
- Underscore
- _.extend({}, source, { a: false })
- ES2016
- { ...source, a: false }
Create a shallow clone of an object(創建一個淺拷貝對象)
- Underscore
- _.clone(object)
- ES2016
- { ...object }
Check if an object is an array(檢查一個對象是否是一個數組)
- Underscore
- _.isArray(object)
- ES5.1
- Array.isArray(object)
Check if an object is a finite Number(檢查一個對象是否是一個有限的數字)
- Underscore
- _.isFinite(object)
- ES2015
- Number.isFinite(object)
Functions(函數)
Bind a function to an object(給對象綁定一個函數)
- Underscore
- foo(function () {
- this.bar();
- }.bind(this));
- foo(_.bind(object.fun, object));
- ES2015
- foo(() => {
- this.bar();
- });
- foo(object.fun.bind(object));
- ES2016
- foo(() => {
- this.bar();
- });
- foo(::object.fun);
Utility(使用功能)
Identity function(迭代行數)
- Underscore
- _.identity
- ES2015
- value => value
A function that returns a value(返回值的函數)
- Underscore
- const fun = _.constant(value);
- ES2015
- const fun = () => value;
The empty function(空函數)
- Underscore
- _.noop()
- ES2015
- () => {}
任何疑問? Send us a pull request on GitHub!
PS:主要內容譯自:https://www.reindex.io/blog/you-might-not-need-underscore