首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

关于闭包跟this的理解

2012-09-20 
关于闭包和this的理解/*closure*/??? var myObject {??? ? num: 2,??? ? add: function(){??? ??? this.n

关于闭包和this的理解

/*closure*/
??? var myObject= {
??? ? num: 2,
??? ? add: function(){
??? ??? this.num=3;
??? ??? (function(){
??? ??? ??? alert(this.num);????? // undefined
??? ??? ??? this.num=4;
??? ??? })();
??? ??? alert(this.num)?????????? // 3
??? ? }
??? }

?


??? /*this.num=3;这个this 是myObject这个对象的一个实例化。
??? 第一个alert(this.num); 中的this 是这个function(){
???? alert(this.num);
???? this.num=4;
??? }对象的一个实例化。那么第一次的alert(this.num); 当执行到它的实惠它并未被定义,所以是undefine.而第二个alert(this.num),在之前被赋值过了所以第二个值是3*/

热点排行