你可能不再需要Underscore
过去几年像 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
你可能不再需要Underscore的更多相关文章
- ES2015 (ES6)
是时候使用ES 2015了 你可能不再需要Underscore BABEL Grunt 先 babel 再用 babel 后的文件 uglify 去掉严格模式.严格模式下全局的this转成了undef ...
- underscore源码解析 (转载)
转载出自http://www.cnblogs.com/human/p/3273616.html (function() { // 创建一个全局对象, 在浏览器中表示为window对象, 在Node.j ...
- underscore源码解析
(function() { // 创建一个全局对象, 在浏览器中表示为window对象, 在Node.js中表示global对象 var root = this; // 保存"_" ...
- Underscore.js 源码学习笔记(下)
上接 Underscore.js 源码学习笔记(上) === 756 行开始 函数部分. var executeBound = function(sourceFunc, boundFunc, cont ...
- 跟着underscore学防抖
前言 在前端开发中会遇到一些频繁的事件触发,比如: window 的 resize.scroll mousedown.mousemove keyup.keydown -- 为此,我们举个示例代码来了解 ...
- Underscore.js部分讲解
underscore是非常好用的封装库,大小只有4KB,大多插件都是以underscore为基础: underscore分5大部分:集合:数组:函数:对象:工具 集合:集合就是伪数组,虽然长的和数组一 ...
- underscore.js源码解析(二)
前几天我对underscore.js的整体结构做了分析,今天我将针对underscore封装的方法进行具体的分析,代码的一些解释都写在了注释里,那么废话不多说进入今天的正文. 没看过上一篇的可以猛戳这 ...
- 利用Underscore求数组的交集、并集和差集
1 数组交集函数——intersection 数组的交集是指包含多个数组中的共同元素的一个数组,求数组的交集就是找出给定数组中的共有元素. 下面实现一个求两个数组交集的函数. 判断数组是够包含指定值, ...
- 理解Underscore中的节流函数
上一篇中讲解了Underscore中的去抖函数(_.debounced),这一篇就来介绍节流函数(_.throttled). 经过上一篇文章,我相信很多人都已经了解了去抖和节流的概念.去抖,在一段连续 ...
随机推荐
- 安装less
1.下载安装iterm(http://www.iterm2.com/) 2.打开iterm,输入 sudo npm install -g less
- 三、jQuery--jQuery基础--jQuery基础课程--第8章 jQuery 实现Ajax应用
1.使用load()方法异步请求数据 使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为:load(url,[data],[callback]) ...
- UISegmentedControl
1. NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3& ...
- CLR via C#(11)-无参属性、有参数属性(索引器)
一. 无参属性 1. 定义属性 无参属性就是我们最常见的属性方式,在赋值时可以加入一定的逻辑判断.属性的定义其实不复杂,先看个直观的例子: 说明: 属性要定义名称和类型,且类型不能是void. 属性是 ...
- MongoDB增删查改
1.insert db.Customers.insert({ "DateTest":new Date(), "IntTest":32, "Double ...
- 攻城狮在路上(贰) Spring(一)--- 软件环境、参考书目等一览表
一.软件环境: 二.参考书目: <Spring 3.X 企业应用开发实战> 陈雄华.林开雄著 电子工业出版社出版 三.其他说明: spring 源码地址:https://github.co ...
- php array_intersect() 和 array_diff() 函数
在PHP中,使用 array_intersect 求两个数组的交集比使用 array_diff 求同样两个数组的并集要快. 如果要求数组 $a 与数组 $b 的差集的个数,应该使用 count($a) ...
- 【转】GeoHash核心原理解析
好久没更新过博客了,先转载一篇文章吧. 源地址:http://www.cnblogs.com/LBSer/p/3310455.html 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按 ...
- 【转】Spark性能优化指南——基础篇
http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&sn=2f413828d1fdc6a ...
- Liferay 6.2 改造系列之二:清理不需要的Portlet
一.特殊Portlet: 以下Portlet数据特殊用途的Portlet,去除后会出现运行错误: 1.站点模版 通过com.liferay.portal.events.AddDefaultLayout ...