Budda
Budda

Reputation: 18343

Can't make enums workable across different file/class in JavaScript

I've created a pretty simple prototype:

<script type="text/javascript">
    Days = { "sunday": 0, "monday": 1, "tuesday": 3, "wednesday": 4,
        "thursday": 5, "friday": 6, "saturday": 7 };
    alert("Day Names Enumerator=" + Days.friday);
</script>

The result of execution is:

Day Names Enumerator=6

Once I moved the enum definition into another file

File1.js:

    Days = { "sunday": 0, "monday": 1, "tuesday": 3, "wednesday": 4,
        "thursday": 5, "friday": 6, "saturday": 7 };

File2.html

<script type="text/javascript" src="File1.js"></script>
<script type="text/javascript">
    alert("Day Names Enumerator=" + Days.friday);
</script>

I've received an error:

Uncaught ReferenceError: Days is not defined

Question1: what is a problem with this approach? I've seen a lot of recommendations regarding enums in JS, but nothing about using them from different files

In the very end, I would like my enum to be a part of the class described in File1:

function File1Class(){
    this.prototype.Days2 = { "sunday": 0, "monday": 1, "tuesday": 3, "wednesday": 4,
        "thursday": 5, "friday": 6, "saturday": 7 };
}

and use it like this:

<script type="text/javascript" src="File1.js"></script>
<script type="text/javascript">
    alert("Day Names Enumerator=" + File1Class.Days2.friday);
</script>

But I still received pretty similar error:

Uncaught TypeError: Cannot read property 'friday' or undefined

Question2: How to use enum defined inside of the class? Again, I've seen a lot of recommendations regarding enums and classes in JS, but nothing about using them both

Please advise, any feedback is welcome.

Thanks a lot in advance.

EDIT:

Why you guys don't make answers? That would help for all (I believe, at least I will be able to vote and reward you somehow).

With regards to the 1st question: I've added 'var' declaration before the 'Days' and that allowed to access its values from another file.

Thanks a lot!

With regards to the 2nd one: it looks like I should move Days2 declaration out of the class constructor to make it executed. I did that: put it before class declaration, after... but still can't even call it from the class itself:

File1Class.prototype.Days2 = { "sunday": 0, "monday": 1, "tuesday": 3, "wednesday": 4,
    "thursday": 5, "friday": 6, "saturday": 7 };
function File1Class(value) {
    this.value = value;
    alert(1);
    alert("<b>From class internals:</b> " + File1Class.prototype.Days2.friday + "<br />");

The error is still the same:

Uncaught TypeError: Cannot read property 'friday' or undefined

Please advise! Any ideas are welcome!

Upvotes: 1

Views: 2177

Answers (1)

Budda
Budda

Reputation: 18343

In "EDIT" section of the answer I've answered to the question 1, for consistency purpose it is:

"var" should be used to define any global variable (thanks to Stoive and jfriend00 for the suggestion)

With regards to the 2nd question: I had another syntax problem in my class file, once it was fixed the proper solution (again, suggested by Stoive) helped:

declaration should not be put into constructor, it should be declared earlier to be executed.

Many thanks to all!

Upvotes: 2

Related Questions