如何使用Javascript将对象参数值传递给同一对象的方法

时间:2022-02-26 16:56:42

Forgive me if this is a potential duplicate, however I have been unable to get any resolution from similar questions on this site or elsewhere.

请原谅我,如果这是一个潜在的重复,但我无法从本网站或其他地方的类似问题得到任何解决方案。

I have created a class with several properties and one method. The values of the properties are obtained from JSON-P data as individual objects are created. The method is supposed to take the value of one of those properties, run it through if-else logic and return a value.

我创建了一个包含多个属性和一个方法的类。在创建单个对象时,从JSON-P数据获取属性的值。该方法应该获取其中一个属性的值,通过if-else逻辑运行它并返回一个值。

The method is not returning a value, and firebug isn't reporting any issues that I can see although I suspect it has something to do with inheritance. Would you be so kind as to examine the code and suggest possible ways to resolve the issue:

该方法没有返回值,并且firebug没有报告我可以看到的任何问题,尽管我怀疑它与继承有关。您是否善于检查代码并提出解决问题的可能方法:

function Forecast(temperature, wind, desc, high, low, iconID, date) {
//creates object class "Forecast"
this.temperature = temperature;
this.wind = wind;
this.desc = desc;
this.high = high;
this.low = low;
this.iconID = iconID;
this.date = date;

this.skycon = function pickIcon() {

                var clearSky = [800];
                var partlyCloudy = [801];
                var cloudy = [802, 803, 804];
                var rain = [200, 201, 202, 210, 211, 212, 221, 230, 231, 232, 300, 301, 302, 310, 311, 312, 313, 314, 321, 500, 501, 502, 503, 504, 511, 520, 521, 522, 531];
                var sleet = [611, 612, 615, 616, 906];
                var snow = [600, 601, 602, 620, 621, 622];
                var windy = [900, 901, 902, 905, 956, 957, 958, 959, 960, 961, 962];
                var misty = [701, 741];

                if (clearSky.indexOf(this.iconID) >= 0) {
                    return Skycons.CLEAR_DAY;   
                } else if (partlyCloudy.indexOf(this.iconID) >= 0) {
                    return Skycons.PARTLY_CLOUDY_DAY;
                } else if (cloudy.indexOf(this.iconID) >= 0) {
                    return Skycons.CLOUD_DAY;
                } else if (rain.indexOf(this.iconID) >= 0) {
                    return Skycons.RAIN;
                } else if (sleet.indexOf(this.iconID) >= 0) {
                    return Skycons.SLEET;
                } else if (snow.indexOf(this.iconID) >= 0) {
                    return Skycons.SNOW;
                } else if (windy.indexOf(this.iconID) >= 0) {
                    return Skycons.WIND;
                } else if (misty.indexOf(this.iconID) >= 0) {
                    return Skycons.MIST;
                }
            };
};

1 个解决方案

#1


0  

I have resolved the issue. The code sample above works precisely as expected. As I am still learning, I neglected to call the method at the appropriate point in the program flow correctly. I did so successfully like this:

我已经解决了这个问题。上面的代码示例正如预期的那样工作。在我还在学习的时候,我忽略了在程序流程中的适当位置正确调用该方法。我成功地这样做了:

skycons.add("icon2", tomorrow.skycon(tomorrow.iconID);

#1


0  

I have resolved the issue. The code sample above works precisely as expected. As I am still learning, I neglected to call the method at the appropriate point in the program flow correctly. I did so successfully like this:

我已经解决了这个问题。上面的代码示例正如预期的那样工作。在我还在学习的时候,我忽略了在程序流程中的适当位置正确调用该方法。我成功地这样做了:

skycons.add("icon2", tomorrow.skycon(tomorrow.iconID);