javascript中的类方法不是函数
Class method in javascript is not a function
答案肯定是显而易见的,但我没有看到它
这是我的javascript类:
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中说:
身份验证。isComponentAvailable不是一个函数
. .
isComponentAvailable
不是附加到(即不是属性)你的对象,它只是由你的功能封闭;所以它是私人的。
你可以用this
作为前缀使它成为公共的
this.isComponentAvailable = function() {
isComponentAvailable实际上是附加在window对象上的
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());
相关文章:
- 使构造函数参数具有ES6类方法的特权
- 从类方法中的 ajax post 函数回调函数更改 javascript 类属性
- 如何将类方法设置为等于多个函数?-Javascript
- 有没有一种方法可以在泛型(<T>)函数/类(Typescript)中获取泛型类型的名称
- Typescript从类的构造函数调用方法
- 当使用Q/promises/异步函数时,如何将一个值从一个类方法返回到另一个类
- ES6 类方法不是函数
- ES6:应用函数作为类方法
- 从 javascript 函数获取类方法
- Javascript子类化,超级构造函数和使用自定义扩展会丢失基类方法
- 从构造函数调用类方法
- 如何在包含函数的方法中使用类变量
- 在类中使用Ecmascript 6箭头函数作为方法的正确方法是什么
- 函数内调用的HTML5 JavaScript画布动画类方法
- 如何从JavaScript函数中调用PHP类方法
- Javascript原型链接超类构造函数和方法调用
- ES6 Javascript类-定义一个现有函数的类方法
- 如何将ES6类方法分配给函数
- 在TypeScript中重写JavaScript函数类中的方法
- 如何在c#(mvc)的类方法中获取JavaScript函数的返回结果