TypedArray.prototype.some() - JavaScript TypedArray 对象
TypedArray.prototype.some()
这个some()
方法检测TypedArray的一些元素是否通过所提供函数的测试.这个方法和Array.prototype.some()
相同.TypedArray是 typed array types 之一.
语法
typedarray.some(callback[, thisArg])
参数
callback
一个测试每个元素的函数,有3个参数:currentValue
在typed array中,正在被测试的元素.index
在typed array中,正在被测试元素的索引.array
正在被调用的 typed array 本身.thisArg
可选的.callback
回调函数的this
值.返回值
true
如果 callback 函数以任一数组元素为参数调用时,返回 truthy;否则,false
.
描述
对于 typed array 中的每个元素,some
方法执行一次callback
,直到找到一个callback
返回 true 的元素.如果一个元素被找到,some
立即返回true
.否则,some
返回false
.
callback
期望3个参数:元素的值,元素的索引,和被遍历的数组对象.
如果some
提供thisArg
,那么thisArg
会作为callback
调用时的this值
.否则,callback
调用时的this
是undefined
.callback
最终可观测的this
是根据确定函数this的通常规则所确定的.
some
被调用不会改变 typed array .
示例
Testing size of all typed array elements
以下示例测试typed array中的所有元素都大于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
Testing typed array elements using arrow functions
Arrow functions 提供更段的语法做相同的测试.
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 的全局对象,必须在“as needed”的基础上进行填充.
// 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
方法填充,因为你不能让它们不可枚举.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!