赞
踩
上篇介绍了typedArray.slice方法,鸿蒙Harmony角落里的知识:从ECMA规范到ArkTS接口(一)本文介绍一个返回结果和参数和slice非常类似的函数:TypedArray.prototype.subarray。
按照ECMA的规范,TypedArray.prototype.subarray(begin, end)
方法定义如下:
如果 end
参数省略,subarray
将默认包含从 begin
开始到原数组结尾的所有元素。如果 begin
或 end
是负值,则它们表示从数组末尾开始的倒数索引。方法返回一个新的 TypedArray
实例,它表示原始 TypedArray
的一个连续子集。
我们可以直接查看ECMA规范中对该函数的描述:
- 23.2.3.30 %TypedArray%.prototype.subarray ( start, end )
- This method returns a new TypedArray whose element type is the element type of this TypedArray and whose ArrayBuffer is the ArrayBuffer of this TypedArray, referencing the elements in the interval from start (inclusive) to end (exclusive). If either start or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.
-
- It performs the following steps when called:
-
- 1. Let O be the this value.
- 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
- 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
- 4. Let buffer be O.[[ViewedArrayBuffer]].
- 5. Let srcRecord be MakeTypedArrayWithBufferWitnessRecord(O, SEQ-CST).
- 6. If IsTypedArrayOutOfBounds(srcRecord) is true, then
- a. Let srcLength be 0.
- 7. Else,
- a. Let srcLength be TypedArrayLength(srcRecord).
- 8. Let relativeStart be ? ToIntegerOrInfinity(start).
- 9. If relativeStart = -∞, let startIndex be 0.
- 10. Else if relativeStart < 0, let startIndex be max(srcLength + relativeStart, 0).
- 11. Else, let startIndex be min(relativeStart, srcLength).
- 12. Let elementSize be TypedArrayElementSize(O).
- 13. Let srcByteOffset be O.[[ByteOffset]].
- 14. Let beginByteOffset be srcByteOffset + (startIndex × elementSize).
- 15. If O.[[ArrayLength]] is AUTO and end is undefined, then
- a. Let argumentsList be « buffer, 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/746661推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。