直接上代码:
function obj(){
this.test1;
this.test2;
this.test3="yeah";
}
obj.test1=function(){
this.v1='hahaha';
var func1=function(){
return "nonono";
};
alert(func1());
}();
obj.test2 = function(){
obj.test1.v1='hehehe';
obj.test3='hahaha';
alert(2);
}();
为什么使用obj.test1.v1时,提示无法获取属性v1,对象未定义或者为null?
望各位指点迷津。JavaScript对象函数
function obj(){
this.test1;
this.test2;
this.test3="yeah";
}
obj.test1=function(){
this.v1='hahaha';
var func1=function(){
return "nonono";
};
alert(func1());
}();
obj.test2 = function(){
obj.test1.v1='hehehe';
obj.test3='hahaha';
alert(2);
}();
为什么使用obj.test1.v1时,提示无法获取属性v1,对象未定义或者为null?
望各位指点迷津。JavaScript对象函数
obj.test1=function(){
this.v1='hahaha';
var func1=function(){
return "nonono";
};
alert(func1());
}();
首先,这里面的this.v1的this指向的是window其次,obj.test1最终的值是undefined,因为代码是先执行立即执行函数,将最终返回的值再赋值给obj.test1。