admin管理员组

文章数量:1291176

I have a Javascript object in an external js file that looks like this:

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:

<script type = "text/javascript">

var someObject = new SomeObj();

</script>

At this point. console.log spits out the UncaughtReference error saying someObj is not defined.

What's wrong ? Help me with this Thanks in advance

I have a Javascript object in an external js file that looks like this:

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:

<script type = "text/javascript">

var someObject = new SomeObj();

</script>

At this point. console.log spits out the UncaughtReference error saying someObj is not defined.

What's wrong ? Help me with this Thanks in advance

Share Improve this question edited Feb 21, 2017 at 5:51 Martin Schneider 3,2684 gold badges20 silver badges30 bronze badges asked Dec 15, 2012 at 1:57 Parijat KaliaParijat Kalia 5,09512 gold badges53 silver badges78 bronze badges 9
  • someObj = new someObj() doesn't look good... – Christophe Commented Dec 15, 2012 at 2:00
  • var someObj = new someObj(); looks better^ – daniel Commented Dec 15, 2012 at 2:01
  • actually I am doing var someObj = new someObj() ; sorry, it is a typo on my part – Parijat Kalia Commented Dec 15, 2012 at 2:04
  • 2 @ParijatKalia: Problem is not just var is that you have two variables with the same exact name. – elclanrs Commented Dec 15, 2012 at 2:05
  • 2 "And in my jQuery," - What jQuery is that? – nnnnnn Commented Dec 15, 2012 at 2:12
 |  Show 4 more ments

1 Answer 1

Reset to default 4

That is because of ambiguous naming of Variable and 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 not defined yet so it spits out an error

本文标签: javascriptUncaught referenceError Object is not definedStack Overflow