简体   繁体   中英

Private and public methods and variable in JavaScript

I am having some problems to understand how to implement and use classes with private/ public methods and use it, even after some read around.

I have the following code:

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

I want "process" and "InitVariables" would be private methods and "getFloors" would be public method, while "Floors", "str" and "Components" would be private vars. I think I made the variable private and "process" and "InitVariables" private, but no success with "getFloor" method.

Right now, "alert(Floors);" shows me the right answer while "alert(Floors);" doesn't show anything. My questions: 1. How can I inplement "getFloors"? 2. Did I write the code well or I should change it?

I haven't test this code but it should help you to understand how to implement a JavaScript class with private and public members:

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM