你可能不再需要Underscore
2016-05-24 14:18
330 查看
过去几年像 Underscore 和 lodash 等库进入许多JavaScript程序员的工具函数中。虽然这些工具库可以使你的代码写起来更容易,但是他们不一定使代码更简单或更容易理解。
各种工具函数库层出不穷,每个工具库的写法也各有不同,这样给阅读和维护你代码的人也带来了一定的困难,以为他必须了解你使用的这个这个工具库的函数做了什么事情。
JavaScript不断发展,新ES2015和ES2016版本(以前分别称为ES6和ES7)包了一堆新功能特性,并很容易使用它们。这些特性使得工具库以前的一些基本功能已经过时。
所以你可能不再需要Underscore。
例子:
这些例子说明,ES5.1,ES2015和ES2016做这些事情很容易,你可能不需要一个实用程序库了。ES5已经得到了所有现代浏览器和node.js的支持,要是想支持传统浏览器(比如IE8),还需要像es-shim这样的帮助脚本。
Arrays(数组)
Iterate(迭代)
Underscore
ES5.1
Map
Underscore
ES5.1
Find(查找)
Underscore
ES2015
Get a property from each element in an array(萃取数组对象中某属性值)
Underscore(注:pluck也许是map最常使用的用例模型的简化版本,即萃取数组对象中某属性值,返回一个数组。)
ES2015
Check if array includes an element(检查数组中是否包含某个元素)
Underscore
ES2016
Convert an array-like object to array(把一个类数组转换成一个数组)
Underscore
ES2015
Create a copy of an array with all falsy values removed.(返回一个除去所有false值的 array副本)
Underscore
ES2015
Create a copy of an array with duplicates removed(返回 array去重后的副本)
Underscore
ES2015
Find the index of a value in an array(查找某个值在 array 中的索引值)
Underscore
ES5.1
Create an array with n numbers, starting from x(创建一个 N个数字数组,从x开始)
Underscore
ES2015
Objects(对象)
Names of own enumerable properties(枚举自身的属性名)
Underscore
ES5.1
Names of all enumerable properties(枚举所有的属性名,包括继承过来的)
Underscore
ES2015
Values(值)
Underscore
ES5.1
Create a new object with the given prototype(创建具有给定原型的新对象)
Underscore
ES5.1
Create a new object from merged properties(创建一个合并属性后的新对象)
Underscore
ES2016
Create a shallow clone of an object(创建一个浅拷贝对象)
Underscore
ES2016
Check if an object is an array(检查一个对象是否是一个数组)
Underscore
ES5.1
Check if an object is a finite Number(检查一个对象是否是一个有限的数字)
Underscore
ES2015
Functions(函数)
Bind a function to an object(给对象绑定一个函数)
Underscore
ES2015
ES2016
Utility(使用功能)
Identity function(迭代行数)
Underscore
ES2015
A function that returns a value(返回值的函数)
Underscore
ES2015
The empty function(空函数)
Underscore
ES2015
任何疑问? Send us a pull request on GitHub!
PS:主要内容译自:https://www.reindex.io/blog/you-might-not-need-underscore
来源:http://www.css88.com/archives/5710
各种工具函数库层出不穷,每个工具库的写法也各有不同,这样给阅读和维护你代码的人也带来了一定的困难,以为他必须了解你使用的这个这个工具库的函数做了什么事情。
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
来源:http://www.css88.com/archives/5710
相关文章推荐
- echart如何设置饼图的颜色以及柱状图颜色
- C++中char类型的溢出问题
- Microsoft Sql server2005的安装步骤图文详解及常见问题解决方案
- 北大教授饶毅500余字毕业典礼致全文
- Android RSA DES等几种加密算法
- 关于tmux和常见vim使用
- Hadoop基础教程之初始Hadoop
- java统计字符串单词的个数
- Apache HttpCore 学习
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- PowerPoint2007将Word 文档作为新演示文稿打开
- 开发中最常用的GitHub上 优秀的 Android 开源项目整理(精品)
- 文本语言模型的参数估计-最大似然估计、MAP及贝叶斯估计
- Django国际化和本地化
- 摘枇杷(最大值最小化)
- SDAutolayout用法大全
- 全局异常捕获
- pp看书笔记---设计模式之禅第二版 第三章【依赖倒置原则】
- _stdcall,_cdecl
- HDU 1394 Minimum Inversion Number 【单节点+逆序数】