3

I have an object cats like this:

var model = {
    cats: [
        {
            name: "Fossie",
            url: "",
            count: 0
        },
        {
            name: "Bootsie",
            url: "",
            count: 0
        }
    ], 
    currentCat: cats[0], //error here
}

When I try to access cats[0], I get following error: ReferenceError: cats is not defined

If I replace cats[0] with this.cats[0] I get this error: Uncaught TypeError: Cannot read property '0' of undefined

Is it not possible to access an object's property within the same object? Or should I create a function inside model and initialize currentCat in there?

software_writer
  • 3,941
  • 9
  • 38
  • 64
  • It is not possible to reference an object from inside its object initializer expression. – Pointy Feb 15 '16 at 20:00
  • 2
    Possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) – stdob-- Feb 15 '16 at 20:04

3 Answers3

2

No, it's not possible. Try this instead:

var model = {
    cats: [
        {
            name: "Fossie",
            url: "",
            count: 0
        },
        {
            name: "Bootsie",
            url: "",
            count: 0
        }
    ]
}
model.currentCat = model.cats[0];

You can't use the object before it has been initialised - so, where you were trying to set currentCat, you were still 'inside' the intialisation, and your model was not accessible.

After this code, model will look like:

model = {
    cats: [
        {
            name: "Fossie",
            url: "",
            count: 0
        },
        {
            name: "Bootsie",
            url: "",
            count: 0
        }
    ],
    currentCat: {
        name: "Fossie",
        url: "",
        count: 0
    }
}
millerbr
  • 2,951
  • 1
  • 14
  • 25
  • I see, thank you for the answer. So I understand the reason it's not working. `cats` is not yet initialized when I do `cats[0]` within model. Right? Also, as per your answer, `model.currentCat = model.cats[0];` will assign `currentcat` property to `model` object and then assign it to first cat, right? – software_writer Feb 15 '16 at 20:10
  • First part is correct. `model.currentCat = model.cats[0];` will set the value of `model.currentCat` to whatever the value is of the first element in your array `model.cats`. The model itself won't be stored. – millerbr Feb 15 '16 at 20:13
  • Is there any way I can contain `currentCat` inside `model`? Just to make it more readable? Thanks – software_writer Feb 15 '16 at 20:15
  • Ah sorry, I should have been more clear - this will indeed contain it inside `model` I'll update my post with an example of what `model` will look like after the code has run – millerbr Feb 15 '16 at 20:16
  • Perfect. I wanted to access `currentCat` as `model.currentCat` from my controller, which I believe this will do. Thank you. – software_writer Feb 15 '16 at 20:19
  • No problem! If the answer is correct, please mark it as such so that future users know to use it! :D – millerbr Feb 15 '16 at 20:20
1

You could store the current cat's index instead (and access the property in a function using this):

var model = {
    cats: [
        {
            name: "Fossie",
            url: "",
            count: 0
        },
        {
            name: "Bootsie",
            url: "",
            count: 0
        }
    ], 
    currentCatIndex: 0,
    getCurrentCat: function() {
        return this.cats[this.currentCatIndex];
    }
}
console.log(model.getCurrentCat());
falsarella
  • 12,217
  • 9
  • 69
  • 115
-1

Dont use it inside the model. Use it after the cats is defined.

var model = {
    cats: [
        {
            name: "Fossie",
            url: "",
            count: 0
        },
        {
            name: "Bootsie",
            url: "",
            count: 0
        }
    ], 
    // currentCat: cats[0], //error here
}
model.currentCat = model.cats[0];

Hopefully this will be good for you

pritesh
  • 533
  • 4
  • 15