Search code examples
javascriptclassobject

Class method in javascript is not a function


The answer must be obvious but I don't see it

here is my javascript class :

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;
        };
    };

then I instanciate

var auth = new Authentification();

alert(Authentification.ACCESS_MASTER);    
alert(auth.componentAvailable);
alert(auth.isComponentAvailable());

I can reach everything but the last method, it says in firebug :

auth.isComponentAvailable is not a function

.. but it is..


Solution

  • isComponentAvailable isn't attached to (ie is not a property of) your object, it is just enclosed by your function; which makes it private.

    You could prefix it with this to make it pulbic

    this.isComponentAvailable = function() {