开发者

Why did I get "Undefined" message?

I have defined a object in a js file:

myobj.js

MyObj={
  test: {
     startTest: function(){
         var x = SOME_PROCESS_A;
         var y = SOME_PROCESS_B;
         return {x: x, y: y};
     }
  }
}

In another js file I call this object function:

other.js

var mytest = MyObj.test.startTest
var a = mytest.x;
var b = mytest.y;

my index.html:

<body>
 <sc开发者_如何学Pythonript src="myobj.js"></script>
 <script src="other.js"></script>
</body>

I got the error from firebug in other.js, "mytest" is undfined in the line "var a = mytest.x;" Why??

Thank you,all. I got another "undefined" problem in the similar code, please check here


You have forgot to call the function:

var mytest = MyObj.test.startTest()


I think you meant to do

var mytest = MyObj.test.startTest(); //calls the function and returns the value to mytest

and not

var mytest = MyObj.test.startTest;//assigns the function to mytest


becouse mytest is a function object, and there are no properties defined in it.

you can either call it like

MyObj.test.startTest();

or rewrite your object something like:

MyObj={
  test: {
     startTest: function(){
         this.x = SOME_PROCESS_A;
         this.y = SOME_PROCESS_B;
         return {x: this.x, y: this.y};
     }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜