[英]Why did I get "Undefined" message?
I have defined a object in a js file:我在 js 文件中定义了一个对象:
myobj.js 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:在另一个 js 文件中,我调用了这个对象函数:
other.js其他.js
var mytest = MyObj.test.startTest
var a = mytest.x;
var b = mytest.y;
my index.html:我的 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??我从萤火误差在other.js,“
mytest
”在行undfined“ var a = mytest.x;
”为什么?
Thank you,all.谢谢你们。 I got another "undefined" problem in the similar code, please check here
我在类似的代码中遇到了另一个“未定义”问题,请在此处查看
你忘记调用函数了:
var mytest = MyObj.test.startTest()
becouse mytest is a function object, and there are no properties defined in it.因为 mytest 是一个函数对象,其中没有定义任何属性。
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};
}
}
}
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.