javascript - 如何使用 JavaScript 访问字典中的本地对象?

标签 javascript dictionary

我有嵌套对象

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

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

};
woeidScript 中的

searchText 返回未定义。如何引用这个本地对象?

最佳答案

你可以使用 getter :

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

一个优点是,您可以将其他值分配给属性 valueAvalueB 并获得除法的实际结果。

-- 以及对对象的直接引用。

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

关于javascript - 如何使用 JavaScript 访问字典中的本地对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36828471/

相关文章:

将 html 移动到父站点后,Javascript 停止工作

css - 获取 html 链接以激活 css 图像映射悬停

c++ - 从类外部将元素插入私有(private)映射

javascript - 为什么在 jQuery 中将事件注册到文档是不好的做法?

javascript - jquery 单击无法正常工作

javascript - 使用 ng-model 绑定(bind)时保留自定义对象类型

python - 字典中的百分比?

javascript - 如何检查用户是否确认自定义模式对话框

python - 在 Django 模板中迭代 dict 时遇到问题

java - 我们需要 TreeMap 的 hashCode 实现吗?