onesiumus
onesiumus

Reputation: 319

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?

Upvotes: 1

Views: 143

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386570

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);

Upvotes: 3

Related Questions