Javascript 未捕获引用错误xxx未定义
为什么我看不到这个问题?有人能帮我吗? 我知道这是一个非常愚蠢的问题。。但我看不到 要执行:Javascript 未捕获引用错误xxx未定义,javascript,jquery,Javascript,Jquery,为什么我看不到这个问题?有人能帮我吗? 我知道这是一个非常愚蠢的问题。。但我看不到 要执行:var xxx=new User(),我总是得到以下结果: ready! VM1507:1 Uncaught ReferenceError: User is not defined(…) 我很抱歉问你 $(function() { console.log( "ready!" ); function User() { this.badgeVervalDatum = null;
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
这一定是个问题
如果在函数内声明变量,则该变量在该函数外不可见。用户类不可访问,因为它是在匿名函数内定义的。 您必须使用户在全局范围内可见。 为此,可以在函数定义之后添加以下行:
window['User'] = User;
您正在通过全局作用域访问用户,但它被声明为
$(function(){}
要获取
User
变量,只需在您的范围内声明它
例如:
var User;
$(function() {
User = function() {/* ... */};
}
new User();`
或者将
User
声明为$(function(){})
范围。函数User()
在该“就绪”中定义处理程序,因此只能从该上下文调用它。它不是全局可用的。您尝试在哪里调用该函数?请注意,User
仅在调用的匿名函数中定义,即$(函数(){函数用户(){..}});新用户()
不行,yeeess!!!!天哪,我太蠢了!我投票以愚蠢结束这个问题,因为我没想清楚。。。