Search code examples
javascriptjavascript-framework

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>
 <script 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


Solution

  • 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