簡體   English   中英

未定義的未捕獲ReferenceError xxx

[英]uncaught ReferenceError xxx is not defined

為什么我看不到問題? 有人可以幫我嗎? 我知道這是一個非常愚蠢的問題..但我看不到..

執行: var xxx = new User() ,我總是這樣:

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

我很抱歉問..

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

也許您在范圍上有問題。 我在$(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

這一定是一個范圍問題。

如果在函數內部聲明變量,則該變量在該函數外部不可見。

User類不可訪問,因為它是在匿名函數中定義的。 您必須使用戶在全局范圍內可見。 為此,您可以在函數定義之后添加以下行:

window['User'] = User;

您正在通過全局作用域訪問User,但已將其聲明為$(function() {}
要獲取User變量,只需在您的范圍內聲明它即可。 閱讀有關js范圍的更多信息

例如:

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

或將User聲明為$(function(){})范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM