Prem
Prem

Reputation: 5997

Difference between Array.method and [].method

Array.from([1,3,10,5], x => x + x); // [2,6,20,10]
[1,2,10,5].from( x => x + x); // TypeError
[1,3,10,5].pop(); // 5

Why different methods of Array are called in different way?

Upvotes: 1

Views: 441

Answers (3)

Pac0
Pac0

Reputation: 23174

In the second case, you want map to return a modified array using a function to apply on each element.

 [1,2,10,5].map(x => x + x); // [2,6,20,10]

And that's actually what you want, in my opinion.


More explanations on Array.from.

This is a function which is 'static', meaning that it cannot be applied on an actual array instance like you tried in the second line, it can be used only on the class Array. Put otherwise in Javascript terms, it's not part of Array.prototype.

Description from the docs to explain the purpose of the function :

From : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

Array.from() lets you create Arrays from:

array-like objects (objects with a length property and indexed elements) or iterable objects (objects where you can get its elements, such as Map and Set).

Array.from() has an optional parameter mapFn, which allows you to execute a map function on each element of the array (or subclass object) that is being created.

More clearly, Array.from(obj, mapFn, thisArg) has the same result as Array.from(obj).map(mapFn, thisArg), except that it does not create an intermediate array.

So, in your case, the use of Array.from(yourArray, mapFunc) seems a bit less clear than simply use youArray.map(mapFunc), because the real interest of using Array.from is when you don't have an array, but something that can be turned into an array.

In this case, it's also convenient to give the mapFunction as second argument to avoid first creating a temporary array, then map the elements to a new final array.

Upvotes: 1

Soron
Soron

Reputation: 448

Given an instance arr of Array, arr.method is equivalent to Array.prototype.method.

Notice the .prototype in there!

So arr.pop() will be the same as Array.prototype.pop.call(arr). But, Array.from has no prototype in there, it's a method directly on the Array constructor itself, and can't (easily) be accessed using an array instance.

If you're familiar with other object-oriented languages (Java, C#, etc.), Array.from is essentially a static method, whereas Array.prototype.pop is essentially an instance method.

Upvotes: 1

Suren Srapyan
Suren Srapyan

Reputation: 68685

from function exists in the Array object itself. So you need to call it via Array instance. You can also create an array with new Array or [] which differs from Array. When you create an array, it has nothing related to the functions of the Array instance. It only has a reference to the Array.prototype in it's prototype chain from which it gets functions and properties such as length or pop. Generally said what is in Array.prototype - are accessible from the array ([]), what is in the Array instance itself - are not and you need to call them via Array instance.

Upvotes: 2

Related Questions