Skip to content

Commit 72db073

Browse files
committed
Add Array.of
1 parent 77f4de5 commit 72db073

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

manuscript/05-Arrays.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,55 @@ TODO
2626

2727
### Array.of()
2828

29+
JavaScript has long had a quirk around creating arrays. The `Array` constructor behaves differently based on the type of data passed to it. For example:
30+
31+
```js
32+
let items = new Array(1, 2); // length is 2
33+
console.log(items.length); // 2
34+
console.log(items[0]); // 1
35+
console.log(items[1]); // 2
36+
37+
items = new Array(2);
38+
console.log(items.length); // 2
39+
console.log(items[0]); // undefined
40+
console.log(items[1]); // undefined
41+
42+
items = new Array("2");
43+
console.log(items.length); // 1
44+
console.log(items[0]); // "2"
45+
```
46+
47+
When the `Array` constructor is passed a single numeric value, that value is set to be the `length` of the array; if a single non-numeric value is passed, then that value becomes the one and only item in the array; if multiple values are passed (numeric or not), then those values become items in the array. This behavior is both confusing and risky, as you may not always be aware of the type of data being passed.
48+
49+
ECMAScript 6 introduces `Array.of()` to solve this problem. `Array.of()` works in a manner that is similar to the `Array` constructor. The only difference is the removal of the special case regarding a single numeric value. The `Array.of()` method always creates an array containing its arguments regardless of the number of arguments or the argument types. Here are some examples:
50+
51+
```js
52+
let items = Array.of(1, 2); // length is 2
53+
console.log(items.length); // 2
54+
console.log(items[0]); // 1
55+
console.log(items[1]); // 2
56+
57+
items = Array.of(2);
58+
console.log(items.length); // 1
59+
console.log(items[0]); // 2
60+
61+
items = Array.of("2");
62+
console.log(items.length); // 1
63+
console.log(items[0]); // "2"
64+
```
65+
66+
The `Array.of()` method is similar to using an array literal, which is to say, you can use an array literal instead of `Array.of()` for native arrays most of the time. If you ever need to pass the `Array` constructor into a function, then you might want to pass `Array.of()` instead to get consistent behavior. For example:
67+
68+
```js
69+
function createArray(arrayConstructor, value) {
70+
return arrayConstructor(value);
71+
}
72+
73+
let items = createArray(Array.of, value);
74+
```
75+
76+
This is a somewhat contrived example, but when dealing with derived array classes or typed arrays, you may find this pattern to be quite useful.
77+
2978
### Array.from()
3079

3180
One of the more cumbersome tasks in JavaScript has long been converting array-like objects into actual arrays. For instance, if you have an `arguments` object (which is array-like) and want to work with it as if it's an array, then you'd need to convert it first. Traditionally, you'd write a function such as:

0 commit comments

Comments
 (0)