Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 绑定、应用和调用方法之间的区别?_Javascript - Fatal编程技术网

Javascript 绑定、应用和调用方法之间的区别?

Javascript 绑定、应用和调用方法之间的区别?,javascript,Javascript,我在stackoverflow和web中搜索,无法得到正确的结果或解释这三种方法之间的差异 据我所知,它们都在不同的上下文中执行相同的函数/方法。 var google = { makeBeer : function(arg1,arg2){ alert([arg1, arg2]); } } google.makeBeer('water','soda'); 这是我的谷歌对象的正常功能。现在,当我在这里使用call和bind

我在stackoverflow和web中搜索,无法得到正确的结果或解释这三种方法之间的差异

据我所知,它们都在不同的上下文中执行相同的函数/方法。

var google = {  
    makeBeer : function(arg1,arg2){     
         alert([arg1, arg2]);        
    }    
}

google.makeBeer('water','soda');
这是我的谷歌对象的正常功能。现在,当我在这里使用call和bind方法时,这里是输出

var google = {
    makeBeer: function (arg1, arg2) {
        alert([arg1, arg2]);
    }
}

google.makeBeer('water', 'soda');

function yahoo() {}

var yah = new yahoo();
google.makeBeer.call(yah, 'pepsi', 'coke');

function msn() {

}

var msn = new msn();
google.makeBeer.call(msn, 'sprite', 'limca');
我仍然看不出这样做的目的,我可以继续用不同的参数调用
google.makeBeer三次。


有人能告诉我更多吗。

bind
使用相同的函数体创建一个新函数,然后返回新函数
call
在不同的传递上下文中调用相同的函数,并且必须显式写入参数
apply
在不同的传递上下文中调用相同的函数,但必须在数组中传递参数

var f = function(p1, p2) {
    var s = this;
}

var newFunc = f.bind(window, 1, 2);
// here newFunc is a function which when you will call will have this as window and p1 = 1 and p2 = 2

f.call(window, 1, 2);
// by executing this line this = window p1 = 1 and p2 = 2

f.call(document, 2, 3);
// by executing this line this = document p1 = 2 and p2 = 3

f.apply(window, [1, 2]);
// by executing this line this = window p1 = 1 and p2 = 2

简单地说,apply()和call()之间没有区别,只是它们之间的区别在于传递的参数。在apply()中,必须将参数作为数组传递,而在call()方法中,必须以逗号分隔的形式传递参数


谈到bind方法,这是ECMAScript 5中引入的新方法,特别用于在调用objects方法时解析
这个
范围
在异步方法调用中特别有用。

应用
调用
是一样的,只是一个接受以数组形式传递给函数的参数,另一个接受以参数形式传递给函数的参数

bind
执行与
call
apply
相同的操作,具体取决于您正在使用的框架,但不会立即调用函数,而是返回一个新函数,将您的参数绑定到此上,当从新范围或上下文调用函数时,
仍将保留您绑定到它的内容。绑定还允许您防止构造函数被
apply
call
攻击,因为无论有人通过
call
apply
发送什么试图覆盖
,它都将始终使用此的绑定参数

以下是一个例子:

function Profile(u) {
    this.user = u;
    this.getUser = function () {
        return this.user;
    };
}

function Profile2(u) {
    this.user = u;
    this.getUser = (function () {
        return this.user;
    });
}

function Profile3(u) {
    this.user = u;
    this.getUser = (function () {
        return this.user;
    });
}

var x = new Profile('guest');
var x2 = new Profile2('guest');
var x3 = new Profile3('guest');

alert(x.getUser.apply({
    user: 'Vinoth'
})); // Vinoth
alert(x2.getUser.call({
    user: 'Babu'
})); // babu
alert(x3.getUser.bind(x3).call({
    user: 'Nandan'
})); // Guest

在您的示例中,In没有什么区别,因为
google.makeBeer
没有使用
this
。当被称为
google.makeBeer(…)
函数中的此
将引用
谷歌
。当被称为
google.makeBeer.call(耶,…)时
将引用
bind
实际上并不执行函数,它创建一个新函数,其中
和一些可选参数绑定到传递的参数。请参阅,我认为DOM方法
.call()
.apply()
之间的主要区别在于可以传递的参数数量(我认为一个只允许一个,另一个更多)。我不确定对
.bind()
的引用是关于什么的,但它通常指的是“绑定”事件及其处理程序。
.bind
不执行该函数。此外,
不是上下文。它是由调用或绑定设置的局部变量。
不是作用域,它(本质上)是由调用设置的局部变量。