function func(){ console.log(this); } var obj={ name:"zhangsan", func:func } //¢Ùͨ¹ýº¯ÊýÃû()µ÷Óõģ¬thisÓÀÔ¶Ö¸Ïòwindow¡£ func(); //¢Úͨ¹ý¶ÔÏó.·½·¨µ÷Óõģ¬thisÖ¸ÏòÕâ¸ö¶ÔÏó obj.func();//ÏÁÒå¶ÔÏó window.onclick=function(){ document.getElementById("div1").onclick=function(){ func();//×îÖÕ»¹ÊÇʹÓÃ()µ÷Óã¬ËùÒÔÖ¸Ïòwindow } document.getElementById("div1").onclick=func;//¹ãÒå¶ÔÏó£¬Ö¸Ïòdiv } arr=[1,2,3,func,4,5,6]; arr[3](); //¢Üº¯Êý×÷ΪwindowÄÚÖú¯ÊýµÄ»Øµ÷º¯ÊýʹÓã¬thisÖ¸Ïòwindow¡£ setTimeout(func,1000); obj1=new func();
ÏÖÔÚ£¬ÄãÒ»¶¨·ÖÇåÁËthisÖ¸ÏòÁË°É£¬ÏÂÃæÎÒÃÇÀ´¿´Ò»¸ö×ۺϰ¸Àý£º
var obj1={
name:"obj1",
arr:[func,1,{name:"obj2",func:func},3,4],
}
obj1.arr[0]();//×îÖյĵ÷ÓÃÕßÊÇÊý×é¡£this-->obj.arr
setTimeout(obj1,arr[0],2000);//obj.arr[0]½ö½öÊÇÈ¡µ½º¯Êý¸³¸øsetTimeout£¬µ«²¢Ã»Óе÷Ó᣺¯ÊýµÄ×îÖÕµ÷ÓÃÕßÊÇsetTimeout¡£Õâ¸öʽ×ÓÏ൱ÓÚsetTimeout(func,2000);
obj1.arr[2].func();//×îÖÕµ÷ÓÃÕßÊÇ{name:"obj2",func:func}
setTimeout(obj1.arr[2].func,2000);
×îºó£¬ÎÒÃÇÔÙÀ´¿´Ò»µÀÃæÊÔÌ⣺
var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: { fullname: 'Aurelio De Rosa', getFullname: function() { return this.fullname; } } }; console.log(obj.prop.getFullname()); test = obj.prop.getFullname; console.log(test()); // º¯ÊýµÄ×îÖÕµ÷ÓÃÕß test() this-> window obj.func = obj.prop.getFullname; console.log(obj.func()); arr = [obj.prop.getFullname,1,2]; arr.fullname = "JiangHao"; console.log(arr[0
Õâ´Î¾Í·ÖÏíµ½ÕâÁË£¬Ï´ÎÔÙ½éÉÜJSÖÐOOPÖеļ̳з½·¨°É~
¡¡