Explicit ".prototype" really needed?

I often see something like this in other people's scripts:

bar = Array.prototype.slice.call(whatever, 1)

However, the following short notation works fine:

bar = [].slice.call(whatever, 1)

Are these two constructions completely equivalent? Are there engines (browsers) that handle them differently?

+5
source share
2 answers

Yes, completely equivalent.

It happens that access through is a .prototypelittle faster, because a new instance of the object should not be created. However, this is what we call microoptimization.


A good way to completely get rid of a deep chain is to call Function.prototype.bind.

Example

(function( slice ) {
    slice( whatever, 1 );
}( Function.prototype.call.bind( Array.prototype.slice )));
+5
source

They are not equivalent strictly speaking. This design:

[].slice.call(whatever, 1)

, . , - .

+1

All Articles