javascript中的类方法不是函数
答案一定很明显,但我看不出来 下面是我的javascript类:javascript中的类方法不是函数,javascript,object,Javascript,Object,答案一定很明显,但我看不出来 下面是我的javascript类: var Authentification = function() { this.jeton = "", this.componentAvailable = false, Authentification.ACCESS_MASTER = "http://localhost:1923", isComponentAvailable = function() {
var Authentification = function() {
this.jeton = "",
this.componentAvailable = false,
Authentification.ACCESS_MASTER = "http://localhost:1923",
isComponentAvailable = function() {
var alea = 100000*(Math.random());
$.ajax({
url: Authentification.ACCESS_MASTER + "/testcomposant?" + alea,
type: "POST",
success: function(data) {
echo(data);
},
error: function(message, status, errorThrown) {
alert(status);
alert(errorThrown);
}
});
return true;
};
};
然后我就开始了
var auth = new Authentification();
alert(Authentification.ACCESS_MASTER);
alert(auth.componentAvailable);
alert(auth.isComponentAvailable());
除了最后一种方法外,我什么都能找到,firebug中写道:
auth.isComponentAvailable不是一个函数。。但事实是
谢谢
isComponentAvailable
没有附加到您的对象(即不是对象的属性),它只是包含在您的函数中;这使得它是私人的
您可以在它前面加上this
,使其成为脉冲状
this.isComponentAvailable=function(){
isComponentAvailable
是一个私有函数。您需要将其添加到this
中以将其公开,如下所示:
var Authentification = function() {
this.jeton = "",
this.componentAvailable = false,
Authentification.ACCESS_MASTER = "http://localhost:1923";
this.isComponentAvailable = function() {
...
};
};
另一种看待它的方式是:
var Authentification = function() {
// class data
// ...
};
Authentification.prototype = { // json object containing methods
isComponentAvailable: function(){
// returns a value
}
};
var auth = new Authentification();
alert(auth.isComponentAvailable());
isComponentAvailable实际上附加到窗口对象。True,但您应该以注释的形式发布,而不是回答