试图获得一个函数的结果来填充类Javascript的参数



我有一个javascript文件,它正在调用一个类。我想获取javascript文件中函数的结果,并将其作为该类的参数传递。我想获取"convertGeoLocation"函数的结果,并将值传递到"Weather"对象中。

// Init weather object
const weather = new Weather();
const ui = new UI();
const convertTypedLocation = new Convert('Redding', 'CA');

// Get weaterh on DOM load
document.addEventListener('DOMContentLoaded', getWeather);
// Get geolocation on load
document.addEventListener('DOMContentLoaded', convertGeoLocation);
function getWeather() {
weather.getWeather()
.then(results => {
// ui.paint(results);
console.log(results);
})
.catch(err => console.log(err))
}
// function getGeoLocation() {
//     getLocation.getGeo()
//     .then(geoResults => {
//         ui.getLocation(geoResults);
//         // console.log(geoResults);
//     })
//     .catch(err => console.log(err));
// }
function convertGeoLocation() {
convertTypedLocation.convertLocation()
.then(convertResults => {
latitude = convertResults.results[0].geometry.lat;
console.log(latitude);
})
.catch(err => console.log(err));
}
function convertGeoLocation() {
convertTypedLocation.convertLocation()
.then(convertResults => {
weather.results = convertResults;
latitude = convertResults.results[0].geometry.lat;
console.log(latitude);
})
.catch(err => console.log(err));
}

请记住,这不是正确的做法。通常最好在天气对象中创建一种新方法,如

addResults(results) {
this.results = results
}

然后通过这种方法添加:

function convertGeoLocation() {
convertTypedLocation.convertLocation()
.then(convertResults => {
weather.addResults(convertResults);
latitude = convertResults.results[0].geometry.lat;
console.log(latitude);
})
.catch(err => console.log(err));
}

最新更新