20

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..

4 Answers 4

22

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() {

Sign up to request clarification or add additional context in comments.

Comments

4

isComponentAvailable is actually attached to the window object.

1 Comment

True, but you should have posted as a comment- not an answer
2

isComponentAvailable is a private function. You need to make it public by adding it to this like so:

var Authentification = function() {
    this.jeton = "",
    this.componentAvailable = false,
    Authentification.ACCESS_MASTER = "http://localhost:1923";

    this.isComponentAvailable = function() {
        ...
    };
};

Comments

2

Another way to look at it is:

var Authentification = function() {
    // class data
    // ...
};

Authentification.prototype = {    // json object containing methods
    isComponentAvailable: function(){
        // returns a value
    }
};

var auth = new Authentification();
alert(auth.isComponentAvailable());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.