你可能不再需要Underscore


過去幾年像 Underscore 和 lodash 等庫進入許多JavaScript程序員的工具函數中。雖然這些工具庫可以使你的代碼寫起來更容易,但是他們不一定使代碼更簡單或更容易理解。

各種工具函數庫層出不窮,每個工具庫的寫法也各有不同,這樣給閱讀和維護你代碼的人也帶來了一定的困難,以為他必須了解你使用的這個這個工具庫的函數做了什么事情。

JavaScript不斷發展,新ES2015和ES2016版本(以前分別稱為ES6和ES7)包了一堆新功能特性,並很容易使用它們。這些特性使得工具庫以前的一些基本功能已經過時。

所以你可能不再需要Underscore。

例子:

這些例子說明,ES5.1,ES2015和ES2016做這些事情很容易,你可能不需要一個實用程序庫了。ES5已經得到了所有現代瀏覽器和node.js的支持,要是想支持傳統瀏覽器(比如IE8),還需要像es-shim這樣的幫助腳本。

Arrays(數組)

Iterate(迭代)

  • Underscore
    1. _.each(array, iteratee)
  • ES5.1
    1. array.forEach(iteratee)

Map

  • Underscore
    1. _.map(array, iteratee)
  • ES5.1
    1. array.map(iteratee)

 

Find(查找)

  • Underscore
    1. _.find(array, predicate)
  • ES2015
    1. array.find(predicate)

Get a property from each element in an array(萃取數組對象中某屬性值)

  • Underscore(注:pluck也許是map最常使用的用例模型的簡化版本,即萃取數組對象中某屬性值,返回一個數組。)
    1. _.pluck(array, propertyName)
  • ES2015
    1. array.map(value => value[propertyName])

Check if array includes an element(檢查數組中是否包含某個元素)

  • Underscore
    1. _.contains(array, element)
  • ES2016
    1. array.includes(element)

Convert an array-like object to array(把一個類數組轉換成一個數組)

  • Underscore
    1. _.toArray(arguments)
  • ES2015
    1. Array.from(arguments)

Create a copy of an array with all falsy values removed.(返回一個除去所有false值的 array副本)

  • Underscore
    1. _.compact(array)
  • ES2015
    1. array.filter(x => !!x)

Create a copy of an array with duplicates removed(返回 array去重后的副本)

  • Underscore
    1. _.uniq(array)
  • ES2015
    1. [...new Set(array)]

Find the index of a value in an array(查找某個值在 array 中的索引值)

  • Underscore
    1. _.indexOf(array, value)
  • ES5.1
    1. array.indexOf(value)

Create an array with n numbers, starting from x(創建一個 N個數字數組,從x開始)

  • Underscore
    1. _.range(x, x + n)
  • ES2015
    1. Array.from({ length: n }, (v, k) => k + x)

Objects(對象)

Names of own enumerable properties(枚舉自身的屬性名)

  • Underscore
    1. _.keys(object)
  • ES5.1
    1. Object.keys(object)

Names of all enumerable properties(枚舉所有的屬性名,包括繼承過來的)

  • Underscore
    1. _.allKeys(object)
  • ES2015
    1. Reflect.enumerate(object) // 返回一個迭代器

Values(值)

  • Underscore
    1. _.values(object)
  • ES5.1
    1. Object.keys(object).map(key => object[key])

Create a new object with the given prototype(創建具有給定原型的新對象)

  • Underscore
    1. _.create(proto, propertiesObject)
  • ES5.1
    1. Object.create(proto, propertiesObject)

Create a new object from merged properties(創建一個合並屬性后的新對象)

  • Underscore
    1. _.extend({}, source, { a: false })
  • ES2016
    1. { ...source, a: false }

Create a shallow clone of an object(創建一個淺拷貝對象)

  • Underscore
    1. _.clone(object)
  • ES2016
    1. { ...object }

Check if an object is an array(檢查一個對象是否是一個數組)

  • Underscore
    1. _.isArray(object)
  • ES5.1
    1. Array.isArray(object)

Check if an object is a finite Number(檢查一個對象是否是一個有限的數字)

  • Underscore
    1. _.isFinite(object)
  • ES2015
    1. Number.isFinite(object)

Functions(函數)

Bind a function to an object(給對象綁定一個函數)

  • Underscore
    1. foo(function () {
    2. this.bar();
    3. }.bind(this));
    4.  
    5. foo(_.bind(object.fun, object));
  • ES2015
    1. foo(() => {
    2. this.bar();
    3. });
    4.  
    5. foo(object.fun.bind(object));
  • ES2016
    1. foo(() => {
    2. this.bar();
    3. });
    4.  
    5. foo(::object.fun);

Utility(使用功能)

Identity function(迭代行數)

  • Underscore
    1. _.identity
  • ES2015
    1. value => value

A function that returns a value(返回值的函數)

  • Underscore
    1. const fun = _.constant(value);
  • ES2015
    1. const fun = () => value;

The empty function(空函數)

  • Underscore
    1. _.noop()
  • ES2015
    1. () => {}

任何疑問? Send us a pull request on GitHub!

PS:主要內容譯自:https://www.reindex.io/blog/you-might-not-need-underscore


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM