JavaScript参考手册
TypedArray
typedArray.some
该some()
方法测试类型数组中的某个元素是否通过了由提供的函数实现的测试。这个方法和Array.prototype.some()
类似。 TypedArray是这里的类型数组类型之一。
语法
typedarray.some(callback[, thisArg])
参数
callback
函数为每个元素测试,取三个参数:currentValue
正在处理的类型数组中的当前元素。index
在类型数组中处理当前元素的索引。被调用的array
数组every
被调用。thisArg
可选的。this
执行时使用的值callback
。
返回值
如果回调函数返回任何数组元素的真值则返回true
; 否则,返回false
。
描述
该some
方法callback
为类型数组中的每个元素执行一次函数,直到找到一个callback
返回真值的函数。如果找到这样的元素,some
立即返回true
。否则,some
返回false
。
callback
被调用三个参数:元素的值,元素的索引和被遍历的数组对象。
如果thisArg
提供了一个参数some
,它将callback
在被调用时传递给它,作为它的this
值。否则,该值undefined
将被传递以用作其this
值。this
最终可观察到的值callback
是根据用于确定this
函数所看到的通常规则来确定的。
some
不会改变它被调用的类型数组。
示例
测试所有类型数组元素的大小
以下示例测试类型数组中的任何元素是否大于10。
function isBiggerThan10(element, index, array) {
return element > 10;
}
new Uint8Array([2, 5, 8, 1, 4]).some(isBiggerThan10); // false
new Uint8Array([12, 5, 8, 1, 4]).some(isBiggerThan10); // true
使用箭头函数测试键入的数组元素
箭头函数为同一个测试提供了一个更短的语法。
new Uint8Array([2, 5, 8, 1, 4]).some(elem => elem > 10); // false
new Uint8Array([12, 5, 8, 1, 4]).some(elem => elem > 10); // true
Polyfill
由于没有名称为TypedArray的全局对象,因此必须在“根据需要”的基础上进行填充。
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
if (!Uint8Array.prototype.some) {
Object.defineProperty(Uint8Array.prototype, 'some', {
value: Array.prototype.some
});
}
如果您需要支持真正过时的不支持的JavaScript引擎,Object.defineProperty
最好不要填充Array.prototype
方法,因为您无法使它们不可枚举。
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray.prototype.some' in that specification. |
Standard |
Initial definition. |
ECMAScript 2017 Draft (ECMA-262)The definition of 'TypedArray.prototype.some' in that specification. |
Draft |
|
浏览器兼容性
Feature |
Chrome |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|
Basic support |
45 |
37 (37) |
No support |
32 |
10 |
Feature |
Android |
Chrome for Android |
Firefox Mobile (Gecko) |
IE Mobile |
Opera Mobile |
Safari Mobile |
---|---|---|---|---|---|---|
Basic support |
No support |
No support |
37 (37) |
No support |
No support |
10 |
TypedArray相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络