Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's is the difference between Array.of(n) , Array(n) and array = [n]?

As the title, i'm wondering what's the difference between this 3 methods of initialization an array.

I'm actually more interested in the new Array.of() method provided from ES6, why they feel the needs of implements that?

like image 384
Stefano Saitta Avatar asked Jan 11 '16 14:01

Stefano Saitta


People also ask

What is the difference between int array [] and int [] array?

They are semantically identical. The int array[] syntax was only added to help C programmers get used to java. int[] array is much preferable, and less confusing. The [] is part of the TYPE, not of the NAME.

What is difference between arr and &arr?

arr is an integer pointer (int*) which points the first element of the array. &arr is an integer array pointer (int*)[5] which points the whole array. (all five elements.) &arr is a pointer to an entire array.

What's difference between array and &array?

Basically, “array” is a “pointer to the first element of array” but “&array” is a “pointer to whole array of 5 int”. Since “array” is pointer to int, addition of 1 resulted in an address with increment of 4 (assuming int size in your machine is 4 bytes).

What is difference between array declaration?

Array declaration tells the compiler about the size and data type of the array so that the compiler can reserve the required memory for the array. This reserved memory is still empty. Array Initialisation assigns values to the array elements i.e. it stores values in the memory reserved for the array elements.


1 Answers

The Array constructor can be called in two ways: a list of values to be used as values for array elements, or with a single numeric value giving the initial length:

var myArray = new Array("hello", "world"); // 2 elements
var otherArray = new Array(100); // 100 elements, all empty

Because there's an ambiguity when just one number is passed, that old API is considered badly designed. Thus, there's Array.of(), which is the same as the first option for the Array constructor:

var otherArray = Array.of(100); // 1 element

The third way to make an array is with an array initialization expression:

var otherArray = [100]; // 1 element

The array instances that are created by each of the above are functionally equivalent and completely interchangeable.

One more thing: why does Array.of() have to exist, given that we can use the array initialization expression? Well, Array.of() is a function, so it can be used as a value applied in functional-style programming. You can (as a slightly dumb example) copy an array with:

var copy = Array.of.apply(Array, original);

One reason that's dumb is that there's also (in ES2015) Array.from() to do the same thing:

var copy = Array.from(original);

That works on any sort of iterable original, so it's a good way to turn arguments or a NodeList into an array.

The MDN site has documentation on Array.of(). The constructor and the array initializer form have been around since forever, so any JavaScript reference will cover those (though possibly without reference to Array.of()).

like image 75
Pointy Avatar answered Oct 18 '22 20:10

Pointy