Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is explicit ".prototype" really needed?

Tags:

javascript

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

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

However, the following shorter notation works fine as well:

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

Are these two constructs fully equivalent? Are there engines (browsers) that treat them differently?

like image 250
georg Avatar asked Apr 11 '13 15:04

georg


1 Answers

Yes, fully equivalent.

It happens to be that the access via .prototype is slightly faster because no new object instance needs to get created. However, thats what we call microoptimization.


A nice way to get entirely rid of deep chaining, is to invoke Function.prototype.bind.

Example

(function( slice ) {
    slice( whatever, 1 );
}( Function.prototype.call.bind( Array.prototype.slice )));
like image 154
jAndy Avatar answered Sep 18 '22 21:09

jAndy