【问题标题】:What does arguments reference when used as an argument?用作参数时,参数引用什么?
【发布时间】:2013-02-25 21:47:50
【问题描述】:

这里是 _.extend from underscore

  // Extend a given object with all the properties in passed-in object(s).
  _.extend = function(obj) {
    each(slice.call(arguments, 1), function(source) {
      if (source) {
        for (var prop in source) {
          obj[prop] = source[prop];
        }
      }
    });
    return obj;
  };

函数 call 需要一个 this 值,后跟一个参数列表。

如果传递的唯一参数是 '1',那么 slice 将返回一个省略第一项的数组。

但是,如何将参数用作我的 MDN 定义的 this 值。

MDN

call

slice

arguments

【问题讨论】:

标签: javascript


【解决方案1】:

使用参数作为 this 值将函数应用于参数。这就像做arguments.slice(1),除了你不能因为arguments在技术上不是一个数组。

【讨论】:

    【解决方案2】:

    它总是引用相同的东西,传递给包含函数的参数。

    【讨论】:

      【解决方案3】:

      我相信“这个”是隐含的。有时您必须“强制”将虚假列表应用于某些 Array 原型,否则它将无法正常工作。

      我可能误解了你的问题,如果是这样,我深表歉意。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-12
        • 1970-01-01
        • 2019-03-27
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多