0

I have this code and I am trying to access 'model' array with '_currentModel' object.

// an array I want to access
var model = [
    {
        name:'haha',
        image:'cat1.jpg',
        count: 0
    },
    {
        name: 'pew',
        image: 'cat2.jpg',
        count: 0
    },
    {
        name: 'wow',
        image: 'cat3.jpg',
        count: 0
    }
];

the object that should access it:

var _currentModel = {
    index: 0,
    name: model[this.index].name, //error: model[this.index] is undefined
    image: model[_currentModel.index].image, //error: _currentModel is undefined
    count: model[_currentModel.index].count //error: _currentModel is undefined

}
Amir Makram
  • 12,030
  • 3
  • 13
  • 25
  • 1
    You are trying to access things before they are created and/or assigned. – sjahan Aug 28 '19 at 08:32
  • 1
    `_currentModel` does not have a value until **after the final `}`** has been processed, and you are trying to access it before that (first using `this.index` and then also `_currentModel.index`). – Peter B Aug 28 '19 at 08:33

0 Answers0