问题
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
回答1:
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
回答2:
You have forgot to call the function:
var mytest = MyObj.test.startTest()
回答3:
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};
}
}
}
来源:https://stackoverflow.com/questions/5699861/why-did-i-get-undefined-message