试图理解underscore.js源代码 - 调用和应用于库中

MMA*_*MMA 7 javascript call apply underscore.js

在Jeremy Ashkenas令人敬畏的Underscore.js库中,我试图理解源文件的一个方面.我不明白:

var slice = Array.prototype.slice;
args = slice.call(arguments, 2);
Run Code Online (Sandbox Code Playgroud)

以便:

args = Array.prototype.slice.call(arguments, 2);
Run Code Online (Sandbox Code Playgroud)

.call或者.apply是功能的方法.但在这里,哪些功能确实.call指的是?第一个参数应该是上下文,但是arguments上下文?第二个参数应该是传递函数的参数.在这里他们是数字2.这是什么意思?有时在图书馆,它使用1或0.它们是传递函数的参数的数量吗?

_.bind = function bind(func, context) {
    var bound, args;
    if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
    if (!_.isFunction(func)) throw new TypeError;
    args = slice.call(arguments, 2);
    return bound = function() {
      if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
      ctor.prototype = func.prototype;
      var self = new ctor;
      var result = func.apply(self, args.concat(slice.call(arguments)));
      if (Object(result) === result) return result;
      return self;
    };
  };
Run Code Online (Sandbox Code Playgroud)

问题2:我不太了解这个功能的逻辑.需要帮助才能理解.一个例子应该非常有用.

  // Invoke a method (with arguments) on every item in a collection.
  _.invoke = function(obj, method) {
    var args = slice.call(arguments, 2);
    return _.map(obj, function(value) {
      return (method.call ? method || value : value[method]).apply(value, args);
    });
  };
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Poi*_*nty 7

Array原型上的"slice"函数期望this它将引用它应该运行的数组.换句话说,如果你有一个真正的数组:

var myArray = [1, 2, 3];
Run Code Online (Sandbox Code Playgroud)

你打电话给slice():

var sliced = myArray.slice(1);
Run Code Online (Sandbox Code Playgroud)

然后在该调用中slice(),this引用数组"myArray".正如雷诺斯在评论中指出的那样:

myArray.slice(1)
Run Code Online (Sandbox Code Playgroud)

是相同的

myArray.slice.call(myArray, 1);
Run Code Online (Sandbox Code Playgroud)

因此,当您使用call()调用函数并将其arguments作为上下文对象传递时,slice()代码将继续运行arguments.传递的其他参数.call()只是其slice()自身的参数或参数.在上面的例子中,请注意我将1传递给函数.

现在关于你的第二个问题,该.invoke()函数首先隔离前两个之后传入的参数.这意味着当你使用时,_.invoke()你传递两个或多个参数:第一个是要操作的列表,第二个是方法,并且(可选的)后续参数被传递给列表中每个元素的方法.

这种呼吁_.map()是复杂的(实际上我认为它有点废话).它正在做的是迭代列表,为列表中的每个值调用一个函数.该函数的作用是首先确定"method"参数是否真的是一个函数.如果是,则它通过.apply()列表元素作为上下文调用该函数.如果"method" 不是函数,则它假定它是每个列表元素的属性的名称,并且属性是函数.

例如,使用简单的列表,它非常简单:

var myList = [1, 2, 3];
var result = _.invoke(myList, function(n) { return this * n; }, 2);
Run Code Online (Sandbox Code Playgroud)

这将得到结果,[2, 4, 6]因为我传递的函数将其上下文对象(this)乘以传递的参数,并且我在调用中传递了2 _.invoke().

使用更复杂的列表,我可以使用第二种风格_.invoke()并在列表中的每个对象上调用一个方法:

var getName = function(prefix) { return prefix + " " + this.name; };
var list = [
  { name: "Bob", getName: getName },
  { name: "Sam", getName: getName },
  { name: "Lou", getName: getName }
];

var result = _.invoke(list, "getName", "Congressman");
Run Code Online (Sandbox Code Playgroud)

这将在列表中的每个对象上调用"getName"函数,并返回从结果中生成的列表.效果将是列表["Congressman Bob", "Congressman Sam", "Congressman Lou"].

现在关于那个废话.在代码中_.invoke():

return _.map(obj, function(value) {
  return (method.call ? method || value : value[method]).apply(value, args);
});
Run Code Online (Sandbox Code Playgroud)

那个subexpresion method || value将永远返回"方法"的值,或者至少几乎总是禁止一些奇特的技巧.如果method.call是真的,那么引用也method必须是真实的.另外,如果是我的代码,我会在回调method 之外进行检查,_.map()以便不必一次又一次地做出决定.也许是这样的:

return _.map(obj, method.call ?
  function(value) { method.apply(value, args); } :
  function(value) { value[method].apply(value, args); }
);
Run Code Online (Sandbox Code Playgroud)

  • `myArray.slice(1)=== slice.call(myArray,1)` (2认同)
  • @Raynos,实际上是假的.`slice`创建两个不同的数组对象,它们不相同. (2认同)
  • 然后他可能不应该使用*code*来表达非正式的平等. (2认同)