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?
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 )));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With