简体   繁体   English

未定义的未捕获ReferenceError xxx

[英]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: 执行: var xxx = new User() ,我总是这样:

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;
       }
   }
});

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() {

    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

It must be a scoping issue. 这一定是一个范围问题。

If you declare variable inside a function, it would not be visible outside that function. 如果在函数内部声明变量,则该变量在该函数外部不可见。

The User class is not accessible because it is defined inside anonymous function. User类不可访问,因为它是在匿名函数中定义的。 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;

You are accessing User via global scope, but it's declared into $(function() {} . 您正在通过全局作用域访问User,但已将其声明为$(function() {}
To get User variable just declare it in your scope. 要获取User变量,只需在您的范围内声明它即可。 Read more about js scopes . 阅读有关js范围的更多信息

For example: 例如:

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

Or declare User into $(function(){}) scope. 或将User声明为$(function(){})范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM