Function 这里的'shiftBind'函数是什么意思?

Function 这里的'shiftBind'函数是什么意思?,function,javascript,Function,Javascript,在javascript代码片段中,我看到了如下代码 nglr.shiftBind = function(_this, _function) { return function() { var args = [ this ]; for ( var i = 0; i < arguments.length; i++) { args.push(arguments[i]); } return _function.apply(_this, args);

在javascript代码片段中,我看到了如下代码

nglr.shiftBind = function(_this, _function) {
  return function() {
    var args = [ this ];
    for ( var i = 0; i < arguments.length; i++) {
      args.push(arguments[i]);
    }
    return _function.apply(_this, args);
  };
};
nglr.shiftBind=function(\u this,\u function){
返回函数(){
var args=[this];
for(var i=0;i
对我来说,这看起来有点抽象。。。谁能举个例子说明这到底是怎么回事


谢谢

这似乎是将当前作用域(当您调用返回函数时)前置到传递到
\u function
的任何参数,然后最后使用指定的作用域(
\u This
)调用它

//获取新的作用域和回调
var shiftBind=函数(\u此,\u函数){
//包装它们,创建闭包
返回函数(){
//调用此函数时,保存当前作用域
var args=[this];
//在任何参数前加上它
for(var i=0;iconsole.log(x.boundFoo());//调用getFoo,它只存在于x上,但打印4个
,这似乎将当前作用域(当您调用返回的函数时)前置到传递到
\u函数
中的任何参数,然后最后使用指定的作用域(
\u This
)调用它

//获取新的作用域和回调
var shiftBind=函数(\u此,\u函数){
//包装它们,创建闭包
返回函数(){
//调用此函数时,保存当前作用域
var args=[this];
//在任何参数前加上它
for(var i=0;iconsole.log(x.boundFoo());//调用getFoo,它只存在于x上,但打印4这是在创建一个新函数,该函数更改原始函数的
This
,但添加原始
This
作为第一个参数。因此,它既具有约束力,又将论点向下移动了一个。我不知道它的用途是什么。

这是创建一个新函数,它更改原始函数的
This
,但添加原始
This
作为第一个参数。因此,它既具有约束力,又将论点向下移动了一个。我不知道它的用途是什么。

此函数可以定义要传递给函数的参数和参数类型(字符串、数字…)。例如:

var nglr = {};

nglr.shiftBind = function(_this, _function) {
  return function() {
    var args = [ this ];
    for ( var i = 0; i < arguments.length; i++) {
      args.push(arguments[i]);
    }
    return _function.apply(_this, args);
  };
};

var obj2 = new Object();
obj2.name = '';
// 1. defined a function that is called when the returned function is executed.
var sumFunc = function(obj, param, param2, param3, param4, param5){
  console.log('obj2: ', this); // obj2
  console.log(obj, param, param2, param3, param4, param5); // Window-Object 1 2 3 4 5
  return param + param2 + param3 + param4 + param5;
};

// 2. hold returned function at variable
var returnedFunc = nglr.shiftBind(obj2, sumFunc);

// 3. call returned function, now all parameters that were given to the returned function are passed on to the sumFunc-function plus as first param the object which context the returned function is executed in(in this case Window, because returnedFunc is not called as member-function). 
returnedFunc(1,2,3,4,5); // 15
它返回10而不是15:

returnedFunc(1,2,3,4,5); // 10
尽管如此,您仍然可以在arguments属性(console.log)中看到第五个参数(5),但它并没有在sumFunc函数中定义为第六个参数。因此返回10,而不是15:

var nglr = {};

nglr.shiftBind = function(_this, _function) {
  return function() {
    var args = [ this ];
    console.log(arguments); // [1, 2, 3, 4, 5]
    for ( var i = 0; i < arguments.length; i++) {
      args.push(arguments[i]);
    }
    return _function.apply(_this, args);
  };
};

此函数可以定义要传递给函数的参数和参数类型(字符串、数字…)。例如:

var nglr = {};

nglr.shiftBind = function(_this, _function) {
  return function() {
    var args = [ this ];
    for ( var i = 0; i < arguments.length; i++) {
      args.push(arguments[i]);
    }
    return _function.apply(_this, args);
  };
};

var obj2 = new Object();
obj2.name = '';
// 1. defined a function that is called when the returned function is executed.
var sumFunc = function(obj, param, param2, param3, param4, param5){
  console.log('obj2: ', this); // obj2
  console.log(obj, param, param2, param3, param4, param5); // Window-Object 1 2 3 4 5
  return param + param2 + param3 + param4 + param5;
};

// 2. hold returned function at variable
var returnedFunc = nglr.shiftBind(obj2, sumFunc);

// 3. call returned function, now all parameters that were given to the returned function are passed on to the sumFunc-function plus as first param the object which context the returned function is executed in(in this case Window, because returnedFunc is not called as member-function). 
returnedFunc(1,2,3,4,5); // 15
它返回10而不是15:

returnedFunc(1,2,3,4,5); // 10
尽管如此,您仍然可以在arguments属性(console.log)中看到第五个参数(5),但它并没有在sumFunc函数中定义为第六个参数。因此返回10,而不是15:

var nglr = {};

nglr.shiftBind = function(_this, _function) {
  return function() {
    var args = [ this ];
    console.log(arguments); // [1, 2, 3, 4, 5]
    for ( var i = 0; i < arguments.length; i++) {
      args.push(arguments[i]);
    }
    return _function.apply(_this, args);
  };
};