Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught referenceError: Object is not defined

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

like image 644
Parijat Kalia Avatar asked Oct 24 '25 14:10

Parijat Kalia


1 Answers

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

like image 50
Sushanth -- Avatar answered Oct 26 '25 04:10

Sushanth --



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!