简体   繁体   English

尝试获取函数的结果以填充类 Javascript 的参数

[英]Trying to get the results of a function to populate the parameters of a class Javascript

I have a javascript file which is calling a class.我有一个正在调用类的 javascript 文件。 I want to take the results of a function in that javascript file and pass it as the parameters of that class.我想获取该 javascript 文件中函数的结果并将其作为该类的参数传递。 I want to take the results of the "convertGeoLocation" function and pass the value into the "Weather" object.我想获取“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));
}

Keep in mind that this is not the proper way of doing it.请记住,这不是正确的做法。 It's usually better to create a new method in the weather object like so :通常最好在天气对象中创建一个新方法,如下所示:

addResults(results) {
    this.results = results
}

and then adding it via this method :然后通过此方法添加它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM