Javascript apply, call this

本文深入解析JavaScript中this关键字的工作原理及使用场景,包括其在不同上下文中指向的变化、function.call与function.apply的区别,并通过实例展示了如何正确使用this来避免常见错误。

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

refer to: http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_this_in_javascript.html

function.apply(thisobj, args)

thisobj: The object to which function is to be applied. In the body of the function, thisobj becomes the value of the this keyword. If this argument is null, the global object is used.
args: An array of values to be passed as arguments to function.

function.call(thisobj, args...)
thisobj: The object on which function is to be invoked. In the body of the function, thisobj becomes the value of the this keyword. If this argument is null, the global object is used.

args...:  Any number of arguments, which will be passed as arguments to function.

<script type="text/javascript">
var flag='Global';
function A(){
    this.flag = 'A';
    this.tip = function(){
       alert(this.flag);
    };
}
function B(){
   this.flag = 'B';
}
var a = new A();
var b = new B();
a.tip.call(b);
a.tip.apply(b);
a.tip.apply(null);
</script>

function.apply()和function.call()调用效果一样, 区别就是输入参数的形式不同

B的实例对象b,成为调用A对象实例a的this, 如果为a.tip.apply(null), 则this为全局对象window


1. In JavaScript this always refers to the “owner” of the function we're executing.
2. The this keyword is relative to the execution context, not the declaration context.

3. For the functions and properties those are not defined in a object, the owner is window


<scripttype="text/javascript">
var a = "windows.a";    
var b="windows.b";  
    
var mydata={  
    a:"mydata.a",
    b:"mydata.b",
    show:function(){  
      var b="show.b";
      alert(this.a);
      alert(this.b);  
    }  
}
mydata.show();
mydata.show.call(window);    
var winObj = mydata.show;  
winObj();
</script>
var someone = {
    name: "Bob",
    showName: function(){
        alert(this.name);
    }
};

var other = {
    name: "Tom",
    showName: someone.showName
}

other.showName();  //Tom
var name = "Tom";

var Bob = {
    name: "Bob",
    show: function(){
        alert(this.name);
    }
}

var show = Bob.show;
show();  //Tom
<script type="text/javascript">
var name = "window";

var Bob = {
    name: "Bob",
    showName: function(){
        alert(this.name);
    }
};

var Tom = {
    name: "Tom",
    showName: function(){
        Bob.showName();
        var fun = Bob.showName;
        fun();
    }
};

Tom.showName();//Bob then window
mydata.show():   the owner of show() is mydata
mydata.show.call(window): window will be this
winObj(): the owner is window
other.showName()内容 fun() doesn't belong to Tom, it belongs to window


Object/prototype/new

function Rectangle(w, h) {
    this.width = w;
    this.height = h;
    this.area = function( ) { return this.width * this.height; }
}

With this new version of the constructor, you can write code like this:

// How big is a sheet of U.S. Letter paper in square inches?
var r = new Rectangle(8.5, 11);
var a = r.area( );

// The constructor function initializes those properties that
// will be different for each instance.
function Rectangle(w, h) {
    this.width = w;
    this.height = h;
}

// The prototype object holds methods and other properties that
// should be shared by each instance.
Rectangle.prototype.area = function( ) { return this.width * this.height; }
var r = new Rectangle(8.5, 11);
Rectangle.prototype.round=function( ) { return 2*(this.width + this.height); }
Rectangle.prototype.width=12;
var a = r.round( );
var xx=new Rectangle(8.5, 11);
var b=xx.round();
alert(a);
alert(b);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值