I have an enum in a file:
goog.provide('animals.Fish');
animals.Fish = function(obj) {
this.name_ = obj[animals.Fish.Properties.NAME];
this.awesomenessLevel_ = obj[animals.Fish.Properties.AWESOMENESS];
}
/**
* Public list of properties.
* @enum {string}
*/
animals.Fish.Properties = {
NAME: 'name',
AWESOMENESS: 'awesomenessLevel',
}
How come I can’t access this enum as a static field of Fish in another class like this?
goog.require('animals.Fish');
...
var tuna = new animals.Fish(
animals.Fish.NAME: 'tuna',
animals.Fish.AWESOMENESS: '100'
)
...
Closure enumeration types are based on the concept of Enum Types from languages such as Java and C++. In Java, enum types are defined as follows:
In your example above,
animals.Fish.Propertiesshould probably be represented as a record type, since the assigned values are not constants. In the example below,animals.Fish.Propertieshas been renamedanimals.Propertiesso that it could be applied to any type of animal (not just fish).fish.js
animals_app.js
On a side note, in the original example there should not be a comma after:
AWESOMENESS: 'awesomenessLevel'in the definition ofanimals.Fish.Properties. In addition, in your second file you need to use the fully qualified enum name. So instead ofanimals.Fish.NAMEit would beanimals.Fish.Properties.NAME.