1

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?

3
  • 1
    You cannot access parts of an object in the middle of its object initializer block. You have to use a separate statement. Commented Apr 24, 2016 at 20:23
  • 1
    searchText references the variable called that. In order to DRY, we would have to look at the code surrounding it. Commented Apr 24, 2016 at 20:24
  • I understand. Thank you! Commented Apr 24, 2016 at 20:25

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.