javascript 未捕获的引用错误:未定义对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13888601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught referenceError: Object is not defined
提问by Parijat Kalia
I have a Javascript object in an external js file that looks like this:
我在外部 js 文件中有一个 Javascript 对象,如下所示:
function SomeObj() {
this.property = 0;
this.property = null;
}
SomeObj.prototype = {
methodA: function() {},
methodB: function() {}
}
In my View files, I load it like this:
在我的视图文件中,我像这样加载它:
<script type ="text/javascript" src="someObj.js"></script>
And in jQuery, I instantiate it like this:
在 jQuery 中,我像这样实例化它:
<script type = "text/javascript">
var someObject = new SomeObj();
</script>
At this point. console.log
spits out the UncaughtReference error saying someObj
is not defined.
这一点。console.log
吐出 UncaughtReference 错误说someObj
未定义。
What's wrong ? Help me with this Thanks in advance
怎么了 ?帮我解决这个提前致谢
回答by Sushanth --
That is because of ambiguous naming of Variable
and Object
那是因为Variable
和Object
someObj = new someObj();
Give it a different name
给它一个不同的名字
var obj1 = new SomeObj();
What happens if you do this
如果你这样做会发生什么
var obj = {
a :a
}
a is notdefined yet so it spits out an error
a 还没有定义,所以它会抛出一个错误