为什么我没有看到问题?有人可以帮我吗? 我知道这是一个非常愚蠢的问题..但我没有看到它......
执行: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;
}
}
});
答案 0 :(得分:2)
也许你的范围有问题。我在$(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
答案 1 :(得分:2)
必须是scoping问题。
如果在函数内部声明变量,则在该函数外部将不会显示该变量。
答案 2 :(得分:0)
无法访问User类,因为它是在匿名函数中定义的。 您必须使用户在全局范围内可见。 为此,您可以在函数定义后添加以下行:
window['User'] = User;
答案 3 :(得分:0)
您正在通过全局范围访问用户,但它已声明为$(function() {}
获取User
变量只需在您的范围内声明它。 Read more about js scopes
例如:
var User;
$(function() {
User = function() {/* ... */};
}
new User();`
或将User
声明为$(function(){})
范围。