未捕获的TypeError:无法设置未定义的属性'getV'

时间:2022-08-24 13:59:57

I have this function in javascript:

我在javascript中有这个功能:

function test(obj,arg1,arg2) {
   ...
   ...
   this.getV=function() {
      var x=2;
      return x; 
   }
}

How can i resolve this problem ?

我该如何解决这个问题?

2 个解决方案

#1


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

#2


This Line:
this.getV()=function(){ var x=2; return x; }

这一行:this.getV()= function(){var x = 2;返回x; }

causes the Error. getV() is undefined! you have to define it like that:

导致错误。 getV()未定义!你必须这样定义它:

this.getV = function(){ var x=2; return x; }

#1


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

#2


This Line:
this.getV()=function(){ var x=2; return x; }

这一行:this.getV()= function(){var x = 2;返回x; }

causes the Error. getV() is undefined! you have to define it like that:

导致错误。 getV()未定义!你必须这样定义它:

this.getV = function(){ var x=2; return x; }