javascript - javascript中的类方法不是函数

标签 javascript class object

答案一定很明显,但我没有看到

这是我的 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 中说:

auth.isComponentAvailable is not a function

..但它是..

最佳答案

isComponentAvailable 没有附加到您的对象(即不是其属性),它只是包含在您的函数中;这使它成为私有(private)的。

你可以在它前面加上 this 让它成为公共(public)的

this.isComponentAvailable = function() {

关于javascript - javascript中的类方法不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16063549/

相关文章:

javascript - Facebook 登录按钮在本地主机上不起作用

javascript - 如何使用 jquery 隐藏基于子类名的表行?

找不到 PHP 命名空间类

javascript - 从 JavaScript 数组中的对象键获取值

javascript - 使电子邮件正则表达式以逗号分隔

javascript - hasOwnProperty ("foo") 总是返回 true,即使我将 foo 分配给 undefined - 为什么?

java - 在面向对象编程中使用类和实例

Java( eclipse ): How to call a int or string from another class in same project?

php - 转换 Javascript 关联对象并在 PHP 中输​​出

php - 将元素添加到其他元素