Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript, is Array.join really Array.prototype.join?

Tags:

javascript

I wonder why even in the Javascript Definitive Guide 6th ed, O'Reilly, p. 149 - 150, it keeps on using Array.join() or Array.concat(). Should it be Array.prototype.join() instead?

But while Chrome doesn't have Array.join defined, Firefox actually does, and it can be invoked by Array.join(array, string). The difference might be important as one is a class method and the other is an instance method. I wonder why that is?

The book kept on using Array.join even in the Core Javascript Reference docs, but maybe it meant Array.prototype.join, and also, it seems to suggest that Array has a length property, but it really should also be a property of Array.prototype, and is Array.prototype.length?

By the way, it seems that Firefox's implementation of Array.prototype.join can be

Array.prototype.join = function(s) {
    return Array.join(this, s);
}

but I don't see that usually being done (defining a class method that can be invoked on an instance).

like image 766
nonopolarity Avatar asked Dec 07 '25 13:12

nonopolarity


1 Answers

FireFox puts a version of prototyped methods on the related constructor.

This isn't part of the ECMAScript standard, but rather is part of the specific JavaScript superset of ECMAScript.

The book should make that distinction, unless the book is talking specifically about the JavaScript extensions.

Keep in mind that JavaScript !== ECMAScript. ECMAScript is the language standard, and JavaScript is an implementation of the 3rd edition of that standard, and includes a superset of functionality not specified by the standard.

like image 78
I Hate Lazy Avatar answered Dec 10 '25 04:12

I Hate Lazy