[英]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. 我希望“ process”和“ InitVariables”是私有方法,“ getFloors”是公共方法,而“ Floors”,“ str”和“ Components”是私有变量。 I think I made the variable private and "process" and "InitVariables" private, but no success with "getFloor" method.
我认为我将变量设置为私有,将“进程”和“ InitVariables”设置为私有,但是使用“ getFloor”方法没有成功。
Right now, "alert(Floors);" 现在,“ alert(Floors);” shows me the right answer while "alert(Floors);"
向我显示正确的答案,同时显示“ alert(Floors);” doesn't show anything.
什么也没显示。 My questions: 1. How can I inplement "getFloors"?
我的问题:1.如何添加“ getFloors”? 2. Did I write the code well or I should change it?
2.我编写的代码是否写得好还是应该更改?
I haven't test this code but it should help you to understand how to implement a JavaScript class with private and public members: 我尚未测试此代码,但它应该可以帮助您了解如何使用私有成员和公共成员来实现JavaScript类:
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 :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.