I was confused when I saw a particular javascript library code - undescore.js
.
I can not remember the snippet of the code, but it was something like what I saw.
var filter = Array.prototype.filter; var meu_array = [1, 2, 3, 4, 5, 6]; filter.call(meu_array, function (value) { return value % 2 == 0; });
That is, it copies the Array.prototype.filter
method to a variable and then calls that copy with call
, to set meu_array
as the context of the hoot.
But, by contrast, why not do with the existing function within array
directly?
meu_array.filter(function (value) { return value % 2 == 0; });
Update : I copied the undescore.js source code that caused the doubt
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
var
push = ArrayProto.push,
slice = ArrayProto.slice,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
Is there any special reason to do this?