Javascript 创建类以了解天气
请帮忙,我创建了一个主类来获取四天的天气,但是控制台抛出了一个错误-getCoordinates没有定义。有什么问题?或者函数init是错误的?我有函数getCoordinates,然后得到城市并返回纬度和经度,然后它应该被传递到函数get WeatherForeCast以获得天气,并从该函数将数据传递到函数renderForeCastInfo以获得四天的天气。怎么了Javascript 创建类以了解天气,javascript,class,asynchronous,promise,Javascript,Class,Asynchronous,Promise,请帮忙,我创建了一个主类来获取四天的天气,但是控制台抛出了一个错误-getCoordinates没有定义。有什么问题?或者函数init是错误的?我有函数getCoordinates,然后得到城市并返回纬度和经度,然后它应该被传递到函数get WeatherForeCast以获得天气,并从该函数将数据传递到函数renderForeCastInfo以获得四天的天气。怎么了 class Weather { async getCoordinates() { //code
class Weather {
async getCoordinates() {
//code
}
async getWeatherForecast(locationCoordinates) {
//code
}
fahrenheitToCelsius(temp) {
//code
}
renderForecastInfo(currently, daily) {
//code
}
init() {
getCoordinates().then(coords => {
return getWeatherForecast(coords);
}).then((forecast) => {
const { currently, daily } = forecast;
renderForecastInfo(currently, daily);
});
}
}
对于成员函数,您必须使用
此
。请参阅下面的代码
班级天气{
建造商(城市){
这个城市
}
异步获取坐标(城市){
//代码
}
异步getWeatherForecast(位置坐标){
//代码
}
华氏摄氏度(温度){
//代码
}
renderForecastInfo(当前,每日){
//代码
}
init(){
this.getCoordinates().then(coords=>{
返回此。getWeatherForecast(coords);
})。然后((预测)=>{
const{current,daily}=预测;
此.renderForecastInfo(当前,每日);
});
}
}
常数w1=新天气();
w1.init()代码>您必须使用此
了解成员函数。请参阅下面的代码
班级天气{
建造商(城市){
这个城市
}
异步获取坐标(城市){
//代码
}
异步getWeatherForecast(位置坐标){
//代码
}
华氏摄氏度(温度){
//代码
}
renderForecastInfo(当前,每日){
//代码
}
init(){
this.getCoordinates().then(coords=>{
返回此。getWeatherForecast(coords);
})。然后((预测)=>{
const{current,daily}=预测;
此.renderForecastInfo(当前,每日);
});
}
}
常数w1=新天气();
w1.init()代码>当我写const w1=newweather('Paris')时,是错误的;-在url中,城市是未定义的部分constURL=
{this.city}&key=08e017c5ff6741f783263868409566648&pretty=1&no_annotations=1&language=en代码>查找另一个问题getWeatherForecast未定义我已编辑代码进行修复,请立即检查。@ameli很高兴它能工作,如果可能,请将此标记为答案,其他人可能会发现它很有用。当我编写const w1=new Weather('Paris')时出现错误;-在url中,城市是未定义的部分constURL=
{this.city}&key=08e017c5ff6741f783263868409566648&pretty=1&no_annotations=1&language=en代码>查找另一个问题getWeatherForecast未定义我已编辑代码进行修复,请立即检查。@ameli很高兴它能工作,如果可能,请将此标记为答案,其他人可能会发现它很有用。