当前位置:首页 » JavaScript技术

Javascript apply, call this

2016-05-24 15:40 本站整理 浏览(6)
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);