0

我正在定义一个htmlARRif/else块中命名的变量。如果我注释掉else块中的变量,它会显示undefined,但如果我不注释掉它,它会显示结果。

注意:我也不能把变量的范围放在 else 里面。

coffeeAllColGenHTML : function (allData, more) {
            console.log("WATER" + allData.WATER);
            console.log("providerWATER" + allData.PROVIDER_WATER);

            if (more) {
                var htmlArr = [];
                htmlArr.push(allData.COFFEE_DISH, "<br/><span class='sub-detail'>", allData.WATER, "</span>");
                return htmlArr.join("");
            } else {
/* this line */ //var htmlArr = [];
                htmlArr.push(allData.COFFEE_DISH, "<br/><span class='sub-detail'>", allData.PROVIDER_WATER, "</span>");
                return htmlArr.join("");
                //return allData.COFFEE_DISH;
                //console.log(allData.COFFEE_DISH);
            }
        },
4

1 回答 1

9

您需要限定变量声明的范围:

coffeeAllColGenHTM: function (allData, more) {
        console.log("WATER" + allData.WATER);
        console.log("providerWATER" + allData.PROVIDER_WATER);
        var htmlArr = [];

        if (more) {
            htmlArr.push(allData.COFFEE_DISH, "<br/><span class='sub-detail'>", allData.WATER, "</span>");
            return htmlArr.join("");
        } else {
            htmlArr.push(allData.COFFEE_DISH, "<br/><span class='sub-detail'>", allData.PROVIDER_WATER, "</span>");
            return htmlArr.join("");
            //return allData.COFFEE_DISH;
            //console.log(allData.COFFEE_DISH);
        }
    },
于 2013-08-14T16:50:47.137 回答