Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove var from TypedArray methods #17790

Merged
merged 14 commits into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The array **iterator** function, which is the
const arr = new Uint8Array([10, 20, 30, 40, 50]);
// your browser must support for..of loop
// and let-scoped variables in for loops
for (let n of arr) {
for (const n of arr) {
console.log(n);
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ The first index of the element in the array; `-1` if not found.
`indexOf` compares `searchElement` to elements of the
typed array using
[strict equality](/en-US/docs/Web/JavaScript/Reference/Operators#using_the_equality_operators)
(the same method used by the ===, or triple-equals, operator).
(the same method used by the `===`, or triple-equals, operator).

## Examples

### Using indexOf

```js
var uint8 = new Uint8Array([2, 5, 9]);
const uint8 = new Uint8Array([2, 5, 9]);
uint8.indexOf(2); // 0
uint8.indexOf(7); // -1
uint8.indexOf(9, 2); // 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ A string with all array elements joined.
### Using join()

```js
var uint8 = new Uint8Array([1,2,3]);
const uint8 = new Uint8Array([1,2,3]);
uint8.join(); // '1,2,3'
uint8.join(' / '); // '1 / 2 / 3'
uint8.join(''); // '123'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ A new _array iterator_ object.
### Iteration using for...of loop

```js
var arr = new Uint8Array([10, 20, 30, 40, 50]);
var eArray = arr.keys();
const arr = new Uint8Array([10, 20, 30, 40, 50]);
const eArray = arr.keys();
// your browser must support for..of loop
// and let-scoped variables in for loops
for (let n of eArray) {
for (const n of eArray) {
console.log(n);
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The last index of the element in the array; `-1` if not found.
### Using lastIndexOf

```js
var uint8 = new Uint8Array([2, 5, 9, 2]);
const uint8 = new Uint8Array([2, 5, 9, 2]);
uint8.lastIndexOf(2); // 3
uint8.lastIndexOf(7); // -1
uint8.lastIndexOf(2, 3); // 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ The `length` property is an accessor property whose set accessor function is `un
### Using the `length` property

```js
var buffer = new ArrayBuffer(8);
const buffer = new ArrayBuffer(8);

var uint8 = new Uint8Array(buffer);
let uint8 = new Uint8Array(buffer);
uint8.length; // 8 (matches the length of the buffer)

var uint8 = new Uint8Array(buffer, 1, 5);
uint8 = new Uint8Array(buffer, 1, 5);
uint8.length; // 5 (as specified when constructing the Uint8Array)

var uint8 = new Uint8Array(buffer, 2);
uint8 = new Uint8Array(buffer, 2);
uint8.length; // 6 (due to the offset of the constructed Uint8Array)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ value would be returned without calling `callbackFn`.
### Sum up all values within an array

```js
var total = new Uint8Array([0, 1, 2, 3]).reduce(function(a, b) {
const total = new Uint8Array([0, 1, 2, 3]).reduce(function(a, b) {
return a + b;
});
// total == 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ value would be returned without calling `callbackFn`.
### Sum up all values within an array

```js
var total = new Uint8Array([0, 1, 2, 3]).reduceRight(function(a, b) {
const total = new Uint8Array([0, 1, 2, 3]).reduceRight(function(a, b) {
return a + b;
});
// total == 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The reversed array.
### Using reverse

```js
var uint8 = new Uint8Array([1, 2, 3]);
const uint8 = new Uint8Array([1, 2, 3]);
uint8.reverse();

console.log(uint8); // Uint8Array [3, 2, 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ beyond the end of the typed array.
### Using set()

```js
var buffer = new ArrayBuffer(8);
var uint8 = new Uint8Array(buffer);
const buffer = new ArrayBuffer(8);
const uint8 = new Uint8Array(buffer);

uint8.set([1, 2, 3], 3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ object's contents will impact the original object and vice versa.
### Using the subarray() method

```js
var buffer = new ArrayBuffer(8);
var uint8 = new Uint8Array(buffer);
uint8.set([1,2,3]);
const buffer = new ArrayBuffer(8);
const uint8 = new Uint8Array(buffer);
uint8.set([1, 2, 3]);

console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ]

var sub = uint8.subarray(0,4);
const sub = uint8.subarray(0, 4);

console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ A string representing the elements of the typed array.
### Using toLocaleString

```js
var uint = new Uint32Array([2000, 500, 8123, 12, 4212]);
const uint = new Uint32Array([2000, 500, 8123, 12, 4212]);

uint.toLocaleString();
// if run in a de-DE locale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ separated by commas. For example, the following code creates a typed array and u
`toString` to convert the array to a string.

```js
var numbers = new Uint8Array([2, 5, 8, 1, 4])
const numbers = new Uint8Array([2, 5, 8, 1, 4])
numbers.toString(); // "2,5,8,1,4"
```

Expand All @@ -51,7 +51,7 @@ If a browser doesn't support the `TypedArray.prototype.toString()` method
yet, JavaScript will call the `toString` method of {{jsxref("Object")}}:

```js
var numbers = new Uint8Array([2, 5, 8, 1, 4])
const numbers = new Uint8Array([2, 5, 8, 1, 4])
numbers.toString(); // "[object Uint8Array]"
```

Expand Down