我有嵌套对象
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.
一个优点是,您可以将其他值分配给属性 valueA
或 valueB
并获得除法的实际结果。
-- 以及对对象的直接引用。
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/