Hi
The Question Is Just The Topic:
How I Can Define Enumeration In JavaScript? :juggle:
Well, what do you want to enumerate?
perhaps you could define enumeration in javascript for us ?
seriously though - what exactly do you mean ?
I think the Enumeration is a clear concept! however, what I want to do is, Defining som modes for my page…
I have a page which can be in some different modes, depending on modes, the tasks differ. Now, I wanr to Define this Modes as Enumeration, to have a clear and flexible code.
Wen I did’nt find, Enumeration, I try to handle it by creating a class consist constant attributes. I follow the way shown in JavaScript Object-Oriented Programming by “Ryan Frishberg” to Create my class and it was some thing like this:
function State_Enum()
{
this.BP_Main = 10;
this.BP_Col = 20;
this.BP_ExCol = 30;
this.FP_LogoTitr_Text = 40;
this.FP_LogoTitr_Photo = 50;
this.FP_MainTitr = 60;
this.FP_ColTitr = 70;
this.FP_ExCol = 80;
this.FP_Aknowledge = 90;
}
but when I use constant keyword, to make the attributes constant, every thing crashs down!!
Now, for example, I can’t use the fileds name in a switch () case Structure, and I have to use its value, and it make some troubles (e.g. when I change the value of some attribute!)
I have another problem too: defining some attribute as a private one! perhaps it’s better to create its own Thread :agree:
Best Regards
I think the Enumeration is a clear concept!
Maybe if someone has studied java, but javascript is not java, and this is a javascript forum, so to expect people that post here to know what an Enumeration is is unrealistic.
but when I use constant keyword, to make the attributes constant, every thing crashs down!!
There is no constant keyword in js–it’s const, and it’s not supported by IE6, nor apparently will FF1.0 allow you to use it to modify a member variable name in a constructor function.
I have another problem too: defining some attribute as a private one!
You can try something like this:
function State_Enum()
{
var BP_Main = 0;
var BP_Col = 0;
this.init = function(mode1, mode2)
{
BP_Main = mode1;
BP_Col = mode2;
}
this.getBP_Main = function(){return BP_Main};
this.getBP_Col = function(){return BP_Col};
}
var se = new State_Enum();
se.init(10, 20);
alert(se.getBP_Main());
Dear 7stud
You are right about Enumeration. I should’nt expect that! sorry… apologize
and you are right about const keyword too.
your guide, for defining private attributs was great. thanks
but it seems that we can’t use Enumeration in JavaScript yet! if this is true, do you know any alternative?!
There is no enum type in javascript, nor does it support constants. However, js has objects literals that look pretty close to C enums
State = {
BP_Main : 10,
BP_Col : 20,
BP_ExCol : 30
}
...
// usage
if(state == State.BP_ExCol) {
The drawbacks of this technique are 1) an explicit value must be provided for every item and 2) none of the items are constant.
Thanks dear stereofrog
It was great!
I didn’t know any think about Object Literals. I found some thing about it. it was very great and useful.
Thanks a lot.