0

Je rencontre des problèmes pour comprendre comment implémenter et utiliser des classes avec des méthodes privées/publiques et les utiliser, même après quelques lectures.Méthodes et variable privées et publiques en JavaScript

je le code suivant:

var _Exits = 0; 
var _Shuttles = 0; 

function Parking(_str) 
{ 
var Floors = 0; 
var str = _str; 
var Components = null; 

function process() 
{ 
    var Exit = new Array(); 
    Exit.push("exit1" + str); 
    Exit.push("exit2"); 
    var Shuttle = new Array(); 
    Shuttle.push("shuttle1"); 
    Shuttle.push("shuttle2"); 
    Components = new Array(); 
    Components.push(Exit, Shuttle); 
} 

function InitVariables() 
{ 
    var maxFloors = 0; 

    _Exits = (Components[0]).length; 
    _Shuttles = (Components[1]).length; 

    /* 
    algorithm calculates maxFloors using Componenets 
    */ 

    Floors = maxFloors; 
} 

//method runs by "ctor" 
process(str); 
InitVariables(); 
alert(Floors); 
} 

Parking.prototype.getFloors = function() 
{ 
return Floors; 
} 

var parking = Parking(fileString); 
alert("out of Parking: " + parking.getFloors()); 

Je veux « processus » et « InitVariables » seraient des méthodes privées et « getFloors » serait méthode publique, alors que les « étages », « str » et « Composants "serait des vars privés. Je pense que j'ai rendu les variables private et "process" et "InitVariables" privées, mais pas de succès avec la méthode "getFloor".

À l'heure actuelle, "alert (Floors);" me montre la bonne réponse tout en "alert (Floors);" ne montre rien. Mes questions: 1. Comment puis-je ajouter "getFloors"? 2. Ai-je bien écrit le code ou devrais-je le changer?

+0

double possible de [Comment définir des variables privées javascript dans le constructeur?] (http://stackoverflow.com/questions/6799103/how-to-set-javascript-private-variables-in-constructor) –

Répondre

1

Je n'ai pas tester ce code, mais il devrait vous aider à comprendre comment implémenter une classe JavaScript avec les membres publics et privés:

var Parking = (function(){ 
    "use strict"; //ECMAScript 5 Strict Mode visit http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ to find out more 

    //private members (inside the function but not as part of the return) 
    var _Exits = 0, 
    _Shuttles = 0, 
    // I moved this var to be internal for the entire class and not just for Parking(_str) 
    _Floors = 0; 

    function process() 
    { 
     var Exit = new Array(); 
     Exit.push("exit1" + str); 
     Exit.push("exit2"); 
     var Shuttle = new Array(); 
     Shuttle.push("shuttle1"); 
     Shuttle.push("shuttle2"); 
     Components = new Array(); 
     Components.push(Exit, Shuttle); 
    }; 

    function InitVariables() 
    { 
     var maxFloors = 0; 
     _Exits = (Components[0]).length; 
     _Shuttles = (Components[1]).length; 

     /* 
     algorithm calculates maxFloors using Componenets 
     */ 
     _Floors = maxFloors; 
    } 

    function Parking(_str) 
    { 
     // Floors was internal to Parking() needs to be internal for the class 
     //var Floors = 0; 
     var str = _str; 
     var Components = null; 
     //method runs by "ctor" 
     process(str); 
     InitVariables(); 
     alert(_Floors); 
    } 

    //public members (we return only what we want to be public) 
    return { 
     getFloors: function() { 
      return _Floors; 
     } 
    } 
}).call(this) 

console.log(Parking.getFloors()) 

Hope it helps :)

Questions connexes