MDN Javascript Bind():

时间:2016-02-15 15:24:31

标签: javascript

给定here的Function.prototype.bind()的一个用途:

  

bind()的下一个最简单的用法是使用   预先指定的初始参数。这些参数(如果有的话)遵循   提供了这个值,然后插入到开头   传递给目标函数的参数,后跟参数   无论何时调用绑定函数,都会传递给bound函数。

问题:为什么未定义绑定在这里?它应该是上下文应该已经发送。如果在这里发生同样的情况,如何定义一个对象?

var leadingThirtysevenList = list.bind(undefined, 37);
function list() {
  return Array.prototype.slice.call(arguments);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

// Create a function with a preset leading argument
var leadingThirtysevenList = list.bind(undefined, 37);

var list2 = leadingThirtysevenList(); // [37]
var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]

1 个答案:

答案 0 :(得分:3)

  

为什么未定义绑定在这里?

因为thislist的值无关紧要(因为list没有引用this),但您必须将参数0指定为某事以指定参数1。

  

如何定义一个对象?

因为JavaScript定义了它。