Javascript 中 apply() 和 call() 的区别

本文详细介绍了JavaScript中call()方法的功能与用法,包括其语法、如何改变函数执行上下文中的this关键字,以及与apply()方法的区别。此外,还探讨了call()方法在构造函数链接和函数绑定中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

call() is a method of function object. It allows you to call (execute) a function (object) as if this function were a method of another object.

Syntax
 fun.call(obj, arg1, arg2, ...);
Scope of this keyword

With the call(), the context of the function fun is executed as if it were the method of object obj. The keyword this in the function fun will be the first argument in call() method obj. If the obj is null or undefined, the value of this keyword will be the global object (Window object). The rest of the arguments starting from the 2nd one in call() method is the arguments which are needed to excecute the function fun.

The following examples help to understand the scope of this keyword.

var name = "Mark";

var fun = function(txt){

       alert(txt + this.name );

};

var obj = {
name: "Tom" };


fun.call(obj, "Hello, ");
 

// return Hello Tom coz this is obj
fun.call(null, "Hello, ");

 // return Hello Mark coz this is the global object

The following code is equivalent to the 1st call above in that fun is defined as a method of obj - obj.me. It's natually to see this keyword in fun is the object obj.

var fun = function (txt){

        alert(txt + this.name );

};


var obj = {

        name: "Tom",
        me: fun
};

obj.me("Hello ");
 // return Hello Tom
Difference between call() and apply()

The apply() method is the similar as the call() method. The difference is that the argument in apply() is given in Array.

Typical Use

Use call() to chain constructors for an object.

function div(width, height) {

    this.width = width;

    this.height = height;

    show.call(this, width, height);

  // executing context changed from div to show,
  // so this changed from div to show   
}

function show() {

    this.css = function () {
 
        alert("{
width: "+this.width + ";
 height: "+ this.height+"}");
 
    };
 
}

var div1 = new div("20px","40px");

div1.css();
 // return {
width: "20px";
 height: "40px"}
Function bind method

One useful application is building the bind method to the prototype of the Function object. The bind method is shared and can be invoked by all functions through inheritance.

Function.prototype.bind = function(obj) {

    var _method = this;

    return function() {

        return _method.apply(obj, arguments);

    };
   
} 

The benefit is that bind returns a function reference that can be used later, rather than the result of an immediate execution that we get with call. Using the bind method, the above code can be written as following,

function div(width, height) {

    this.width = width;

    this.height = height;

    var _show = show.bind(this);

    _show(width, height);

// show is the function to be executed rather than the function name
// _show is the binded function reference of the function of show
// with this keyword changed to div object
   
}

var show = function () {

    this.css = function () {
 
        alert("{
width: "+this.width + ";
 height: "+ this.height+"}");
 
    };
 
}

var div1 = new div("20px","40px");

div1.css();
 // return {
width: "20px";
 height: "40px"}

本文来源:http://www.pagecolumn.com/javascript/apply_call.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值