Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class method in javascript is not a function

The answer must be obvious but I dont 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..

Thank you

like image 636
mlwacosmos Avatar asked Apr 17 '13 15:04

mlwacosmos


2 Answers

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

like image 165
dm03514 Avatar answered Sep 22 '22 20:09

dm03514


isComponentAvailable is actually attached to the window object.

like image 41
Stone Shi Avatar answered Sep 21 '22 20:09

Stone Shi