8
I was confused when I saw a certain javascript library code - the undescore.js
.
I don’t really remember the code part, 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 method Array.prototype.filter
for a variable and then calls that copy with call
, to define meu_array
as part of the.
But, on the other hand, why not do with the function existing within the array
directly?
meu_array.filter(function (value) { return value % 2 == 0; });
Updating: I found a passage from 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?