includes方法 使用 Object.defineProperty 扩展 ,解决不兼容问题
2017-07-24 11:04
726 查看
原文链接 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
如果
如果你需要支持那些不支持
Mobile
includes()方法用来判断一个数组是否包含一个指定的值,如果是,酌情返回 true或 false。
let a = [1, 2, 3]].includes(2); // true a.includes(4); // false
语法EDIT
arr.includes(searchElement) arr.includes(searchElement, fromIndex)
参数
searchElement需要查找的元素值。
fromIndex可选从该索引处开始查找
searchElement。如果为负值,则按升序从 array.length + fromIndex的索引开始搜索。默认为 0。
返回值
一个Boolean。
示例EDIT
[1, 2, 3].includes(2)]// true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
fromIndex
大于等于数组长度
如果fromIndex大于等于数组长度 ,则返回
false。该数组不会被搜索。
var arr = ['a', 'b', 'c']].includes('c', 3); //false arr.includes('c', 100); // false
计算出的索引小于 0
如果fromIndex为负值,计算出的索引将作为开始搜索
searchElement的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
// 数组长度是3
// fromIndex是 -100
// computed index 是 3 + (-100) = -97
var arr = ['a', 'b', 'c']].includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
includes()
作为一个通用方法
includes()方法有意设计为通用方法。它不要求
this值是数组对象,所以它可以被用于其他类型的对象 (比如类数组对象)。下面的例子展示了 在函数的arguments对象上调用的
includes()方法。
(function() { console.log([].includes.call(arguments, 'a'))]// true console.log([].includes.call(arguments, 'd')); // false })('a','b','c');
PolyfillEDIT
// https://tc39.github.io/ecma262/#sec-array.prototype.includes if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined')]}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndexis undefined, this step produces the value 0.)
var n = fromIndex| 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
// NOTE: === provides the correct "SameValueZero" comparison needed here.
if (o[k] === searchElement) {
return true;
}
k++;
}
// 8. Return false
return false;
}
});
}
如果你需要支持那些不支持
Object.defineProperty的废弃JavaScript 引擎,你最好不要 polyfill
Array.prototype方法,因为你不能使它们不可枚举。
规范EDIT
Specification | Status | Comment |
---|---|---|
ECMAScript 2016 (ECMA-262) Array.prototype.includes | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) Array.prototype.includes | Draft |
浏览器兼容性EDIT
DesktopMobile
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 47 | 43 (43) | 未实现 | 14 | 34 | 9 |
相关文章推荐
- 再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题)
- 原型扩展的方法解决IE和Firefox的Js兼容问题
- IE兼容问题解决根源:深刻理解hasLayout属性,原理与使用方法
- IE兼容问题解决根源:深刻理解hasLayout属性,原理与使用方法
- jquery 和其他js框架同时使用的不兼容问题解决方法
- (转)再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题)
- [译]因扩展Object.prototype而引发Object.defineProperty不可用的一个问题
- 在安装并使用tensorflow中的Object-detection模块时遇到的一些问题汇总及解决方法
- jquery 和其他js框架同时使用的不兼容问题解决方法
- inline-block和text-indent在IE6,IE7下同时使用的兼容问题解决方法
- JavaScript使用Object.defineProperty方法实现双数据绑定
- JS进阶篇--JS apply的巧妙用法以及扩展到Object.defineProperty的使用
- IE兼容问题解决根源:深刻理解hasLayout属性,原理与使用方法
- 使用php缺少加密扩展mcrypt等相关问题的一些解决方法
- 使用ld的“-Ttext”选项时可能产生的一个问题的解决方法
- PHP4与PHP3中一个不兼容问题的解决方法
- [Nebula2]使用3dmax7,nmaxtoolbox导出模型到nebula2遇到的问题及解决方法
- 使用webservice解决多系统登陆问题(方法一)
- Eclipse的SWT使用ViewForm,ToolBar,ToolItem兼容问题解决
- Firefox 使用常见问题和解决方法