Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Array.function and Array.prototype.function?

I've found that some functions like concat() push() every() both exist in Array and Array.prototype(with firefox 57.0.1 console)

It's confusing since prototype methods exist in Array.
Additionally, where does staic method(Array.from(), Array.isArray() etc) exists in?

enter image description here


I think I've understood concepts of javascript prototype to some extent, so what I'm curious about is why prototype methods(concat() push() ...) apear both in Array and Array.prototype

like image 759
soonoo Avatar asked Dec 07 '17 08:12

soonoo


People also ask

What is the prototype of array function?

prototype allows you to add new properties and methods to arrays. prototype is a property available with all JavaScript objects.

What is the difference between array and method?

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.

How do you make a prototype array?

The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will available for every array. When constructing a property, All arrays will be given the property, and its value, as default.

Can I use array at ()?

Array.prototype.at()The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.


1 Answers

Firefox's Array function appears to have additional (non-conformant) "static" methods that replicate the prototype methods except that they take the array as the first parameter instead of via the implicit this context.

To see those methods and properties of Array, use:

Object.getOwnPropertyNames(Array)

In Firefox you'll (mostly) see the same list as in your first screenshot. I haven't yet figured out why Array.isArray is missing in your list, but it does appear in my Firefox 57.

In Chrome you'll only see the ES6 mandated "static" methods (i.e. Array.from, Array.isArray, Array.of) and the standard properties.

like image 58
Alnitak Avatar answered Oct 19 '22 17:10

Alnitak