Javascript 未捕获类型错误:无法设置属性';getV&x27;未定义的
我在javascript中有这个函数:Javascript 未捕获类型错误:无法设置属性';getV&x27;未定义的,javascript,function,this,Javascript,Function,This,我在javascript中有这个函数: function test(obj,arg1,arg2) { ... ... this.getV=function() { var x=2; return x; } } 如何解决此问题?此行: function test(obj,arg1,arg2) { ... ... this.getV = function() { // getV() is wrong you are declari
function test(obj,arg1,arg2) {
...
...
this.getV=function() {
var x=2;
return x;
}
}
如何解决此问题?此行:function test(obj,arg1,arg2) {
...
...
this.getV = function() { // getV() is wrong you are declaring function
var x = 2;
return x;
}
}
var testObj = new test();
testObj.getV(); // return 2
this.getV()=function(){var x=2;return x;}
导致错误的原因getV()未定义!你必须这样定义它:
this.getV = function(){ var x=2; return x; }
你能提供更多的代码吗?你能解释一下这个应该指什么(或者你认为它应该指什么)吗?