Javascript的Bind实现? [英] Javascript's Bind implementation?

查看:60
本文介绍了Javascript的Bind实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 bind 不是跨浏览器(旧的)函数,因此有一个polyfill :(来自John Resig的预订

Since bind is not a cross browser (old ones) function , there is a polyfill for it : ( from John Resig's book)

/*1*/       Function.prototype.bind = function ()
/*2*/       {
/*3*/           var fn = this,
/*4*/               args = Array.prototype.slice.call(arguments),
/*5*/               object = args.shift();
/*6*/           return function ()
/*7*/           {
/*8*/               return fn.apply(object,
/*9*/                   args.concat(Array.prototype.slice.call(arguments)));
/*10*/           };
/*11*/       };

但我不明白为什么我们需要参数在行#9

But I don't understand why do we need arguments at line #9.

我的意思是:

如果我有这个对象:

var foo = {
    x: 3
}

我有这个功能:

var bar = function(p,b){

    console.log(this.x+'        '+p+'   '+b);
}

所以,如果我想要 foo 上下文中运行,带参数 - 所有我需要做的是:

So , if I want bar to run in the foo context , with parameters - All I need to do is :

 var boundFunc = bar.bind(foo,1,2)
 boundFunc ()...

所以当我运行 var.bind(foo,1,2) arguments [object Object],1,2

这些参数被保存第4行。

Those arguments are saved at line #4.

很棒。

现在, bind 函数返回自己的闭包函数:

Now , the bind function returns its own closured function :

function ()
    {
        return fn.apply(object,
            args.concat(Array.prototype.slice.call(arguments)));
    } 

问题

为什么我们需要参数?它似乎是这样的:

Why do we need arguments here ? it seems that they are for something like :

var boundFunc = bar.bind(foo,1,2)
boundFunc (more1,more2....) //<----- ??

我错过了什么吗?

Oonce我设置了第一个 var boundFunc = bar.bind(foo,1,2),我已经宣布参数。为什么我们需要它们两次?

Oonce I set the first var boundFunc = bar.bind(foo,1,2) , I already declared the parameters. why do we need them twice ?

推荐答案

有两个地方可以将参数传递给绑定函数:

There are two places you can pass in arguments to the bound function:

1)当你调用bind(第一个参数)时。这些在调用时总是应用于绑定函数。

1) When you call bind (the first arguments). These are always applied to the bound function when it is called.

2)调用绑定函数时(第二个参数)。这些是你提到的more1,more2。这些更改取决于调用绑定参数时提供的内容。

2) When you call the bound function (the second arguments). These are the "more1, more2" that you mention. These change depending on what is provided when the bound argument is called.

第9行将原始绑定参数与提供的额外参数组合。

Line 9 is combining the original bound arguments with the supplied extra arguments.

我想你可能会混淆的概念是你最初不必绑定所有参数 - 你只能绑定上下文对象,或者你也可以绑定第一个参数但是绑定函数的调用者提供其余的函数。例如:

I guess the concept you might be confused about is that you don't have to bind ALL arguments initially - you can bind just the context object, or you can bind the first one argument as well but have callers of the bound function supply the rest. For example:

function sum() {
 var _sum = 0
 for (var i = 0; i < arguments.length ; i++) {
     _sum += arguments[i];
 }
 return _sum;
}
var sum_plus_two = sum.bind({},2);
sum_plus_two(5,7) == 14;

这篇关于Javascript的Bind实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆