谢谢你们所做的一切,我第一次就做对了,但由于其他原因,我一直感到困惑,并试图修复一些没有损坏的东西,哈哈。
最佳答案
第一个有效,因为您正在尝试访问已初始化的全局数据对象。在你传递的构造函数中已经有了描述
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data.weather[0].description.toUpperCase());`
function Weather(cityName, description) {
this.cityName = cityName
this.description = description;
this._temperature = '';
}
在第二种情况下,您尝试访问 this.data.weahter
,但对象中没有任何 this.data
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data);
function Weather(cityName, datacity) {
this.cityName = cityName
this.data.weather[0].description.toUpperCase() = description;
// ^ here your don't have data object, also neithier the weather for accessing by index
this._temperature = '';
}
我认为您只是输错了属性名称,您需要
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data);
function Weather(cityName, datacity) {
this.cityName = cityName
this.description = datacity.weather[0].description.toUpperCase();
this._temperature = '';
}
关于javascript - 无法读取天气应用程序中未定义 JavaScript 的属性 'weather',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42113582/