谢谢大家,我第一次做对了,但是由于其他原因,我一直感到困惑,并试图修复没有坏的东西。

最佳答案

第一个起作用是因为您正在尝试访问已初始化的全局数据对象。在构造函数中,您通过的描述已经

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的属性“天气”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42113582/

10-10 11:43