87,915
社区成员
发帖
与我相关
我的任务
分享
function x1(){
object.getName = 'x1';
}
function x2(){
var object.getName = 'x2';
}
这两个有什么不一样。
function xx1(){
object.getName = function(){
return this.name;
}
}
function xx2(){
object.getName= object.getName;
}
这两个是不是一样
abc.fn() // this ===abc
xyz.fn() // this === xyz
//fn点号前是哪个,this就指向哪个,没有就指向 window
var name ="the window"; //window
var object={
name :"my object",
getName:function(){
return this.name;
}
}
var fn=object.getName
fn()
var fn2 =function(){
return this.name;
}
fn2();
[/quote]大侠,fn2好理解,相当于window.fn2,所以this指代window,
但是下面这个,还是my object,怎么理解
var name="The Window";
var object={
name:"my Object",
getName:function (){
return this.name;
}
}
object.getName=object.getName;
console.log(object.getName());
var name ="the window"; //window
var object={
name :"my object",
getName:function(){
return this.name;
}
}
var fn=object.getName
fn()
var fn2 =function(){
return this.name;
}
fn2();