2013-04-17 4 views
7

नहीं है इस सवाल का जवाब स्पष्ट होना चाहिए लेकिन मैं इसेकक्षा विधि एक समारोह

यहाँ

देख न है मेरी जावास्क्रिप्ट वर्ग:

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

तो मैं instanciate

var auth = new Authentification(); 

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

मैं कर सकते हैं आखिरी विधि तक पहुंचें, लेकिन यह अंतिम विधि है, यह फायरबग में कहता है:

auth.is ComponentAvailable एक समारोह नहीं है .. लेकिन यह है ..

धन्यवाद

उत्तर

11

isComponentAvailable से जुड़ी नहीं है अपने वस्तु, यह सिर्फ अपने कार्य से घिरा है (यानी की संपत्ति नहीं है); जो इसे निजी बनाता है।

आप this अवश्य लगा दें यह pulbic

this.isComponentAvailable = function() {

2

isComponentAvailable एक निजी समारोह है बनाने के लिए कर सकता है। तुम इतनी तरह 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

सच है, लेकिन आपको एक टिप्पणी के रूप में पोस्ट किया जाना चाहिए था - उत्तर नहीं –

संबंधित मुद्दे