[基础] Array.prototype.indexOf()查询方式
背景
最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数。
其中store.subscribe(listener)用于注册一个listener,同时返回一个unsubscribe方法,用于注销当前注册的listener。
源码中查询listener索引时用到了Array.indexOf方法,如下:

一直用indexOf做值类型数组的查询,故对于此种情况记录下
Array.prototype.indexOf函数
搜索Array对象的指定元素并返回该元素的索引
var index = [].indexOf(item, start);
| 参数 | 描述 |
|---|---|
| item | 要查询的元素 |
| start | 开始检索的索引 |
此外还可以使用其静态方法
Array.indexOf(array,item,start)
调用indexOf,如果查询到结果,返回对应的索引;否则返回-1
关于检索元素的类型
indexOf可以对一些简单类型的数组进行检索

但如果数组中元素是对象类型,是否也可以用indexOf进行检索呢?比较的依据是什么呢?

通过这组结果可以看出,若使用indexOf检索对象数组,比较的依据实际只进行指针相等判定,而不会比较是否对象中各个值相等。
基于以上,我们也可以用这种方法检索存在数组中的方法,就如Redux源码中的用法

总结
Array.prototype.indexOf可以进行数组的检索,对于值类型数组,通过值比较确定相等;对于对象数组,通过指针比较确定相等
[基础] Array.prototype.indexOf()查询方式的更多相关文章
- Array.prototype.indexOf
arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()
- 使用Array.prototype.indexOf()的几点注意
对应indexOf这个方法,在日常开发中比较常见的应该是String.prototype.indexOf()方法,Array.prototype.indexOf()方法和其有很大的相似性,本文不想去描 ...
- 有了 indexOf,为什么 ECMAScript 7 还添加了 Array.prototype.include
ECMAScript 7 中新增了用于检测数组中是否包含某个元素 Array.prototype.includes() API,想到了 Array 其实有很多相关 API 可以检测到是否包含某个元素, ...
- Javascript实现Linq查询方式
Linq是.net平台一个重要的技术,全称Language Integrated Query.通过构建快速查询语句,可快速从数据库或集合中筛选数据集.以查询数据库相同的方式操作内存数据. 在ECMAS ...
- 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法
/* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...
- JS Array常用方法indexOf/filter/forEach/map/reduce详解
Array共有九个方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.protot ...
- 为Array 添加indexOf
为array赋予属性 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (elt /*, from*/) { var ...
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...
- 数组方法 Array.prototype
Object.prototype 数组的值是有序的集合,每一个值叫做元素,每一个元素在数组中都有数字位置编号,也就是索引,js中数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或者 ...
随机推荐
- jquery获取dropdownlist的value和text值
1.jquery //获取value值 $("#ddlSubmodel").val(); //获取text值 $("#ddlSubmodel").find(&q ...
- 四种解析和创建方式(DOM,SAX,DOM4J,JDOM)
一.先导入jar包 DOM基于树形,SAX基于事件,DOM4J和JDOM基于底层API 二.代码如下 1 package com.sxt.test; import java.io.File; impo ...
- Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39
Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39 V8 重构iocutilV4,use def iocFact...jettyUtil V ...
- ASP模拟POST请求异步提交数据的方法
这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...
- CString转换为LPSTR和LPSTR转化为CString
一.CString转换为LPSTR 方法一: CString strFileName LPSTR lpstr - strFileName.GetBuffer(); strFileName.Releas ...
- GridView的各种属性
<GridView android:id="@+id/movie_list" android:layout_width="906dp" android:l ...
- Images.xcassets不能获取图片路径
原文地址:http://www.jianshu.com/p/5358f587af38 Images.xcassets在app打包后,以Assets.car文件的形式出现在bundle中.其作用在于: ...
- 用collectionview实现瀑布流-转(后面附demo,供参考)
算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...
- Linux下,拷贝文件时,排除某些文件
一下是自己用到到几次实践,觉得很赞: 1.拷贝文件时,排除某些不需要的文件: 1)使用xargs来做: ls /tmp/test/ |grep -v .gz |xargs -i cp -r ...
- easyui表格的增删改查
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...