Skip to content

Commit

Permalink
#2 Make all TypedArray constructor examples consistent (mdn#17831)
Browse files Browse the repository at this point in the history
* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md

* Update index.md
  • Loading branch information
Garcia authored Jul 1, 2022
1 parent 919c0fd commit 5b776a0
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,23 @@ console.log(bigint64.length); // 2
console.log(bigint64.BYTES_PER_ELEMENT); // 8

// From an array
const arr = new BigInt64Array([21n,31n]);
console.log(arr[1]); // 31n
const x = new BigInt64Array([21n, 31n]);
console.log(x[1]); // 31n

// From another TypedArray
const x = new BigInt64Array([21n, 31n]);
const y = new BigInt64Array(x);
console.log(y[0]); // 21n

// From an ArrayBuffer
const buffer = new ArrayBuffer(32);
const z = new BigInt64Array(buffer, 0, 4);
const buffer = new ArrayBuffer(64);
const z = new BigInt64Array(buffer, 8, 4);
console.log(z.byteOffset); // 8

// From an iterable
const iterable = function*(){ yield* [1n, 2n, 3n]; }();
const bigint64_2 = new BigInt64Array(iterable);
// BigInt64Array[1n, 2n, 3n]
const iterable = function*() { yield* [1n, 2n, 3n]; }();
const bigint64FromIterable = new BigInt64Array(iterable);
console.log(bigint64FromIterable);
// BigInt64Array [1n, 2n, 3n]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,23 @@ console.log(biguint64.length); // 2
console.log(biguint64.BYTES_PER_ELEMENT); // 8

// From an array
const arr = new BigUint64Array([21n,31n]);
console.log(arr[1]); // 31n
const x = new BigUint64Array([21n, 31n]);
console.log(x[1]); // 31n

// From another TypedArray
const x = new BigUint64Array([21n, 31n]);
const y = new BigUint64Array(x);
console.log(y[0]); // 21n

// From an ArrayBuffer
const buffer = new ArrayBuffer(32);
const z = new BigUint64Array(buffer, 0, 4);
const buffer = new ArrayBuffer(64);
const z = new BigUint64Array(buffer, 8, 4);
console.log(z.byteOffset); // 8

// From an iterable
const iterable = function*(){ yield* [1n, 2n, 3n]; }();
const biguint64_2 = new BigUint64Array(iterable);
// BigUint64Array[1n, 2n, 3n]
const iterable = function*() { yield* [1n, 2n, 3n]; }();
const biguint64FromIterable = new BigUint64Array(iterable);
console.log(biguint64FromIterable);
// BigUint64Array [1n, 2n, 3n]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,30 @@ new Float32Array(buffer, byteOffset, length);

```js
// From a length
let float32 = new Float32Array(2);
const float32 = new Float32Array(2);
float32[0] = 42;
console.log(float32[0]); // 42
console.log(float32.length); // 2
console.log(float32.BYTES_PER_ELEMENT); // 4

// From an array
const arr = new Float32Array([21,31]);
console.log(arr[1]); // 31
const x = new Float32Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Float32Array([21, 31]);
const y = new Float32Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(16);
const z = new Float32Array(buffer, 0, 4);
const buffer = new ArrayBuffer(32);
const z = new Float32Array(buffer, 4, 4);
console.log(z.byteOffset); // 4

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const float32 = new Float32Array(iterable);
// Float32Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const float32FromIterable = new Float32Array(iterable);
console.log(float32FromIterable);
// Float32Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,30 @@ new Float64Array(buffer, byteOffset, length);

```js
// From a length
let float64 = new Float64Array(2);
const float64 = new Float64Array(2);
float64[0] = 42;
console.log(float64[0]); // 42
console.log(float64.length); // 2
console.log(float64.BYTES_PER_ELEMENT); // 8

// From an array
const arr = new Float64Array([21,31]);
console.log(arr[1]); // 31
const x = new Float64Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Float64Array([21, 31]);
const y = new Float64Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(32);
const z = new Float64Array(buffer, 0, 4);
const buffer = new ArrayBuffer(64);
const z = new Float64Array(buffer, 8, 4);
console.log(z.byteOffset); // 8

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const float64 = new Float64Array(iterable);
// Float64Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const float64FromIterable = new Float64Array(iterable);
console.log(float64FromIterable);
// Float64Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,23 @@ console.log(int16.length); // 2
console.log(int16.BYTES_PER_ELEMENT); // 2

// From an array
const arr = new Int16Array([21,31]);
console.log(arr[1]); // 31
const x = new Int16Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Int16Array([21, 31]);
const y = new Int16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(8);
const z = new Int16Array(buffer, 0, 4);
const buffer = new ArrayBuffer(16);
const z = new Int16Array(buffer, 2, 4);
console.log(z.byteOffset); // 2

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const int16 = new Int16Array(iterable);
// Int16Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const int16FromIterable = new Int16Array(iterable);
console.log(int16FromIterable);
// Int16Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,23 @@ console.log(int32.length); // 2
console.log(int32.BYTES_PER_ELEMENT); // 4

// From an array
const arr = new Int32Array([21,31]);
console.log(arr[1]); // 31
const x = new Int32Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
let x = new Int32Array([21, 31]);
const y = new Int32Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(16);
const z = new Int32Array(buffer, 0, 4);
const buffer = new ArrayBuffer(32);
const z = new Int32Array(buffer, 4, 4);
console.log(z.byteOffset); // 4

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const int32_from_iterable = new Int32Array(iterable);
// Int32Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const int32FromIterable = new Int32Array(iterable);
console.log(int32FromIterable);
// Int32Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,23 @@ console.log(int8.length); // 2
console.log(int8.BYTES_PER_ELEMENT); // 1

// From an array
const arr = new Int8Array([21,31]);
console.log(arr[1]); // 31
const x = new Int8Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Int8Array([21, 31]);
const y = new Int8Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(8);
const z = new Int8Array(buffer, 1, 4);
console.log(z.byteOffset); // 1

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const int8_from_iterable = new Int8Array(iterable);
// Int8Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const int8FromIterable = new Int8Array(iterable);
console.log(int8FromIterable);
// Int8Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,6 @@ new Uint16Array(buffer, byteOffset, length);
`length` is omitted, the remainder of
`buffer` is viewed.

## Description

The **`Uint16Array()`** typed array constructor creates an
array of 16-bit unsigned integers in the platform byte order. If control over byte order
is needed, use {{jsxref("DataView")}} instead. The contents are initialized to
`0`. Once established, you can reference elements in the array using the
object's methods, or using standard array index syntax (that is, using bracket
notation).

Starting with ECMAScript 2015, `Uint16Array` constructors require to be
constructed with a {{jsxref("Operators/new", "new")}} operator. Calling a
`Uint16Array` constructor as a function without `new`, will throw
a {{jsxref("TypeError")}} from now on.

```js example-bad
const dv = Uint16Array([1, 2, 3]);
// TypeError: calling a builtin Uint16Array constructor
// without new is forbidden
```

```js example-good
const dv = new Uint16Array([1, 2, 3]);
```

## Examples

### Different ways to create a Uint16Array
Expand All @@ -91,22 +67,23 @@ console.log(uint16.length); // 2
console.log(uint16.BYTES_PER_ELEMENT); // 2

// From an array
const arr = new Uint16Array([21,31]);
console.log(arr[1]); // 31
const x = new Uint16Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Uint16Array([21, 31]);
const y = new Uint16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(8);
const z = new Uint16Array(buffer, 0, 4);
const buffer = new ArrayBuffer(16);
const z = new Uint16Array(buffer, 2, 4);
console.log(z.byteOffset); // 2

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const uint16_from_iterable = new Uint16Array(iterable);
// Uint16Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const uint16FromIterable = new Uint16Array(iterable);
console.log(uint16FromIterable);
// Uint16Array [1, 2, 3]
```

## Specifications
Expand All @@ -117,6 +94,23 @@ const uint16_from_iterable = new Uint16Array(iterable);

{{Compat}}

### Compatibility notes

Starting with ECMAScript 2015, `Uint16Array` constructors require to be
constructed with a {{jsxref("Operators/new", "new")}} operator. Calling a
`Uint16Array` constructor as a function without `new`, will throw a
{{jsxref("TypeError")}} from now on.

```js example-bad
const dv = Uint16Array([1, 2, 3]);
// TypeError: calling a builtin Uint8ClampedArray constructor
// without new is forbidden
```

```js example-good
const dv = new Uint16Array([1, 2, 3]);
```

## See also

- [Polyfill of `Uint16Array` in `core-js`](https://github.com/zloirock/core-js#ecmascript-typed-arrays)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,30 @@ new Uint32Array(buffer, byteOffset, length);

```js
// From a length
var uint32 = new Uint32Array(2);
const uint32 = new Uint32Array(2);
uint32[0] = 42;
console.log(uint32[0]); // 42
console.log(uint32.length); // 2
console.log(uint32.BYTES_PER_ELEMENT); // 4

// From an array
var arr = new Uint32Array([21,31]);
console.log(arr[1]); // 31
const x = new Uint32Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
var x = new Uint32Array([21, 31]);
var y = new Uint32Array(x);
const y = new Uint32Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(16);
var z = new Uint32Array(buffer, 0, 4);
const buffer = new ArrayBuffer(32);
const z = new Uint32Array(buffer, 4, 4);
console.log(z.byteOffset); // 4

// From an iterable
var iterable = function*(){ yield* [1,2,3]; }();
var uint32 = new Uint32Array(iterable);
// Uint32Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const uint32FromIterable = new Uint32Array(iterable);
console.log(uint32FromIterable);
// Uint32Array [1, 2, 3]
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,23 @@ console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1

// From an array
const arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31
const x = new Uint8Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const x = new Uint8Array([21, 31]);
const y = new Uint8Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(8);
const z = new Uint8Array(buffer, 1, 4);
console.log(z.byteOffset); // 1

// From an iterable
const iterable = function*(){ yield* [1,2,3]; }();
const uint8Arr = new Uint8Array(iterable);
// Uint8Array[1, 2, 3]
const iterable = function*() { yield* [1, 2, 3]; }();
const uint8FromIterable = new Uint8Array(iterable);
console.log(uint8FromIterable);
// Uint8Array [1, 2, 3]
```

## Specifications
Expand Down
Loading

0 comments on commit 5b776a0

Please sign in to comment.