```
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, 𝔽(beginByteOffset) ».
16. Else,
a. If end is undefined, let relativeEnd be srcLength; else let relativeEnd be ? ToIntegerOrInfinity(end).
b. If relativeEnd = -∞, let endIndex be 0.
c. Else if relativeEnd < 0, let endIndex be max(srcLength + relativeEnd, 0).
d. Else, let endIndex be min(relativeEnd, srcLength).
e. Let newLength be max(endIndex - startIndex, 0).
f. Let argumentsList be « buffer, 𝔽(beginByteOffset), 𝔽(newLength) ».
17. Return ? TypedArraySpeciesCreate(O, argumentsList).
This method is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.
```
```
/**
* Creates a Uint8Array with the same underlying Buffer
*
* @param begin start index, inclusive
*
* @param end last index, exclusive
*
* @returns new Uint8Array with the same underlying Buffer
*/
public subarray(begin?: number, end?: number): Uint8Array {
return this.subarray(asIntOrDefault(begin, 0 as int), asIntOrDefault(end, this.lengthInt))
}
/**
* Creates a Uint8Array with the same underlying Buffer
*
* @param begin start index, inclusive
*
* @param end last index, exclusive
*
* @returns new Uint8Array with the same underlying Buffer
*/
public subarray(begin: number, end: number): Uint8Array {
return this.subarray(begin as int, end as int)
}
/**
* Creates a Uint8Array with the same underlying Buffer
*
* @param begin start index, inclusive
*
* @param end last index, exclusive
*
* @returns new Uint8Array with the same underlying Buffer
*/
public subarray(begin: number, end: int): Uint8Array {
return this.subarray(begin as int, end as int)
}
/**
* Creates a Uint8Array with the same underlying Buffer
*
* @param begin start index, inclusive
*
* @param end last index, exclusive
*
* @returns new Uint8Array with the same underlying Buffer
*/
public subarray(begin: int, end: number): Uint8Array {
return this.subarray(begin as int, end as int)
}
/**
* Creates a Uint8Array with the same underlying Buffer
*
* @param begin start index, inclusive
*
* @param end last index, exclusive
*
* @returns new Uint8Array with the same underlying Buffer
*/
public subarray(begin: int, end: int): Uint8Array {
const len: int = this.length as int
const relStart = normalizeIndex(begin, len)
const relEnd = normalizeIndex(end, len)
let count = relEnd - relStart
if (count < 0) {
count = 0
}
return new Uint8Array(this.buffer, relStart * Uint8Array.BYTES_PER_ELEMENT as int, count)
}
/**
* Creates a Uint8Array with the same Buffer
*
* @param begin start index, inclusive
*
* @returns new Uint8Array with the same Buffer
*/
public subarray(begin: number): Uint8Array {
return this.subarray(begin as int, this.lengthInt)
}
/**
* Creates a Uint8Array with the same Buffer
*
* @param begin start index, inclusive
*
* @returns new Uint8Array with the same Buffer
*/
public subarray(begin: int): Uint8Array {
return this.subarray(begin, this.lengthInt)
}
```
const success = 0;
const fail = 1;
function testSubarrayWithOutParam(): int {
let source: number[] = [10, 20, 30, 40, 50, 60];
let ss = new ArrayBuffer(source.length as int * 1);
if (target.length as int != origin.length as int) {
console.log("Array length mismatch on slice");
return fail;
}
//Check all the data copied;
for (let i: int = 0; i< origin.length as int; i++) {
let tv = target as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
origin= new Uint8Array(0);
if (origin.length as int != 0){
return fail;
}
if (target.length as int != origin.length as int - subarrayStart) {
console.log("Array length mismatch on subarray One Params" + target.length);
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != origin.length as int) {
console.log("Array length mismatch on subarray One Params" + target.length);
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
//The subarray method returns a view of the original array, so modifications made to the subarray will affect the original array, and vice versa.
target[subarrayStart] = 1;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
origin[subarrayStart] = 2;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
return success;
}
function testSubarrayTwoParams(): int {
let source: number[] = [10, 20, 30, 40, 50, 60, 70, 80];
let ss = new ArrayBuffer(source.length as int * 1);
if (target.length as int != subarrayEnd - subarrayStart) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != subarrayEnd - subarrayStart) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != subarrayEnd - subarrayStart) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != subarrayEnd - subarrayStart) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != subarrayEnd - subarrayStart) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = subarrayStart; i< subarrayEnd; i++) {
let tv = target[i - subarrayStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if (target.length as int != 0) {
console.log("Array length mismatch on subarray2");
return fail;
}
return success;
}
function testSubarrayTwoParamsWithOtherNumber(): int {
let source: number[] = [10, 20, 30, 40, 50, 60, 70, 80];
let ss = new ArrayBuffer(source.length as int * 1);
if (target.length as int != subarrayEnd - (origin.length + subarrayStart)) {
console.log("Array length mismatch on subarray2");
return fail;
}
//Check all the data copied;
for (let i: int = (origin.length + subarrayStart) as int; i< subarrayEnd; i++) {
let tv = target[i - (origin.length + subarrayStart)] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if (tv != ov) {
console.log("Array data mismatch");
return fail;
}
}