2013-04-17 9 views
7

はありませんが、私はここにクラスメソッドは、答えは明白でなければならない機能

にそれを参照してくださいいけない私の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()); 

をインスタンス化します最後の方法を除いてすべてに達する、それは火かき棒で言う:

auth.is ComponentAvailableは機能ではありません..しかし、それは..です

(すなわちのプロパティではありません)

答えて

11

isComponentAvailableが接続されていないありがとう、あなたのオブジェクト、それはちょうどあなたの関数で囲まれています。プライベートにする

あなたはそれpulbic

this.isComponentAvailable = function() {

2

isComponentAvailableはプライベート関数であることを確認するためにthisを前に付けできます。あなたはそのようthisに追加して公開する必要があります。

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

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

それを見て別の方法は次のとおりです。

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

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

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

isComponentAvailableは実際にウィンドウオブジェクトに取り付けられています。

+4

真実ですが、コメントではなく回答として投稿してください –

関連する問題