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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:03:51  来源:igfitidea点击:

Uncaught referenceError: Object is not defined

javascriptjqueryjavascript-objects

提问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.logspits out the UncaughtReference error saying someObjis 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 Variableand Object

那是因为VariableObject

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 还没有定义,所以它会抛出一个错误