Can someone help me understand how to access enums.ROCK
within the prop in the code snippet below? I keep getting an error that says
Uncaught TypeError: Cannot read property 'enums' of undefined
. Please pay attention to the comment ERROR LINE
.
const app = {
enums: {
ROCK: 'ROCK',
PAPER: 'PAPER',
SCISSORS: 'SCISSORS',
DEFAULT_USER_CHOICE: this.ROCK //default, remember this keyword
},
prop: {
isGameRunning: false,
mySelection: this.app.enums.ROCK //ERROR LINE
}
};
I've attempted using mySelection: app.enums.ROCK
, which resulted in the error
Uncaught ReferenceError: Cannot access 'app' before initialization
.
Click the following links for examples:
Sample 1: https://jsfiddle.net/5zp7yode/
Sample 2: https://jsfiddle.net/5zp7yode/1/
However, the following example works perfectly fine:
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
alert(person.fullName());
Sample 3: https://jsfiddle.net/0o93afLq/
I also tried the following approach without success:
const app = {
self: this,
enums: {
ROCK: 'ROCK',
PAPER: 'PAPER',
SCISSORS: 'SCISSORS',
DEFAULT_USER_CHOICE: this.ROCK //default, remember this keyword
},
prop: {
isGameRunning: false,
mySelection: self.enums.ROCK
}
};
alert(app.prop.mySelection);
Sample 4: https://jsfiddle.net/x89kj2zn/
I also have a working solution using a function but facing issues calling it inside the prop
:
const app = {
enums: {
ROCK: 'ROCK',
PAPER: 'PAPER',
SCISSORS: 'SCISSORS',
DEFAULT_USER_CHOICE: this.ROCK //default, remember this keyword
},
prop: {
isGameRunning: false
},
getMySelection: function(){
return app.enums.ROCK;
}
};
alert(app.getMySelection());
Sample 5: https://jsfiddle.net/v5qzdx3s/