javascript - 无法读取天气应用程序中未定义 JavaScript 的属性 'weather'

标签 javascript json

谢谢你们所做的一切,我第一次就做对了,但由于其他原因,我一直感到困惑,并试图修复一些没有损坏的东西,哈哈。

最佳答案

第一个有效,因为您正在尝试访问已初始化的全局数据对象。在你传递的构造函数中已经有了描述

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/

相关文章:

javascript - :eq() selector returns undefined

javascript - 为什么在输入用户输入时过滤无法正常工作?

javascript - Jquery 从 span 中删除文本

javascript - 如何在 Jquery POST 中获取 HTTP 错误响应代码

json - 如何使用 swift 4 解码这个 JSON

javascript - 来自 Facebook 的 React Native Navigator 示例代码失败 : type is invalid: expected a string (for built-in components) or a class/function

ios - 空 JSON 响应

json - 使用 JSON 序列化 Gtk TreeStore/ListStore

JSON - 格式化最佳实践

javascript - 计算父 JSON 对象内的对象数量