mannerofallthings mannerofallthings - 7 months ago 7
Javascript Question

How to access a local object in a dictionary with JavaScript?

I have nested object

var model = {
weather: {
allData: ""
},

woeid: {
id: 2389646,
searchText: "davis",
woeidScript: "some string'"+searchText+"' another string",
forcastScript: "",
found: true
}

};


searchText
in
woeidScript
returns undefined. How can reference this local object?

Answer

You could use a getter:

The get syntax binds an object property to a function that will be called when that property is looked up.

An advantage is, you can assign other values to property valueA or valueB and get the actual result of the division.

-- And a direct reference to the object.

var model = {
    weather: {
        allData: ""
    },
    woeid: {
        id: 2389646,
        searchText: "davis",
        get woeidScript() { return "some string'" + model.woeid.searchText + "' another string"; },
        forcastScript: "",
        found: true
    }
};
document.write(model.woeid.woeidScript);