user4431269
user4431269

Reputation:

uncaught ReferenceError xxx is not defined

Why do I not see the problem? Can someone help me out? I know it is a very stupid question.. but I do not see it..

to execute: var xxx = new User() and I always get this:

ready!
VM1507:1 Uncaught ReferenceError: User is not defined(…)

I'm sorry to ask..

$(function() {
   console.log( "ready!" );

   function User() {
       this.badgeVervalDatum = null;
       this.bijNaam = null;
       this.contactVia = null;
       this.email = null;
       this.familieNaam = null;
       this.gsm = null;
       this.id = null;
       this.middleNaam = null;
       this.postcode = null;
       this.rkNummer = null;
       this.sanBekw = null;
       this.straat = null;
       this.voorNaam = null;
       this.volledigNaam = null;
       this.woonplaats = null;
       this.timeCreated = 0;
       this.timeUpdate = 0;
       this.timeLastLogin = 0;
   }

   User.prototype = {
       constructor: User,
       addEmail: function(email) {
           this.email = email;
           return true;
       }
   }
});

Upvotes: 2

Views: 6480

Answers (4)

Artem Yavorsky
Artem Yavorsky

Reputation: 23

You are accessing User via global scope, but it's declared into $(function() {}.
To get User variable just declare it in your scope. Read more about js scopes.

For example:

var User;
$(function() {
  User = function() {/* ... */};
}
new User();`

Or declare User into $(function(){}) scope.

Upvotes: 0

Igor Bukin
Igor Bukin

Reputation: 1006

The User class is not accessible because it is defined inside anonymous function. You have to make the User be visible in global scope. To do that you can add the following line right after function definition:

window['User'] = User;

Upvotes: 0

It must be a scoping issue.

If you declare variable inside a function, it would not be visible outside that function.

Upvotes: 1

Ali Mamedov
Ali Mamedov

Reputation: 5256

Maybe you have a problems with scope. I you define your constructor and prototype in $(function() { ... }) they will not be visible outside of this block.

$(function() {

    function User() {
        this.badgeVervalDatum = null;
        this.bijNaam = null;
        this.contactVia = null;
        this.email = null;
        this.familieNaam = null;
        this.gsm = null;
        this.id = null;
        this.middleNaam = null;
        this.postcode = null;
        this.rkNummer = null;
        this.sanBekw = null;
        this.straat = null;
        this.voorNaam = null;
        this.volledigNaam = null;
        this.woonplaats = null;
        this.timeCreated = 0;
        this.timeUpdate = 0;
        this.timeLastLogin = 0;
    }

    User.prototype = {
        constructor: User,
        addEmail: function(email) {
            this.email = email;
            return true;
        }
    }    

    var user = new User(); // this is ok
});  

var user = new User();  // this will not work

Upvotes: 1

Related Questions