从嵌套函数返回对象?

时间:2013-08-04 13:02:20

标签: javascript function object angularjs

我试图将函数内部的对象作为另一个函数内的对象返回:

    function () {
    // body...
var dailyPrice = null; 
                function fun1(xml) {
                /***function**/
                }

                function fun2(xml) {
                /*function***/
                }
                function getRate(source, $scope) {
                    var dateValue = $("Date", source).text() || "";
                    if (!dateValue) {
                        return null;
                    }
                    var dailyPrice = $("DailyPrice", source).text() || "";
                    var weeklyPrice = $("WeeklyPrice", source).text() || "";
                    var monthlyPrice = $("MonthlyPrice", source).text() || "";
                    var isAvailable = $("IsAvailable", source).text() === "1";
                    var minimumStay = Number($("MinimumStay", source).text());
                    if (isNaN(minimumStay)) {
                        minimumStay = DEFAULT_MINIMUM_STAY;
                    }

                    return {
                        date: new Date(dateValue),
                        dailyPrice: dailyPrice,
                        weeklyPrice: weeklyPrice,
                        monthlyPrice: monthlyPrice,
                        reserved: !isAvailable,
                        minimumStay: minimumStay
                    };

                }
               return {
                    getallrates: function(source, $scope){
                        getRate(source, $scope);
                        console.log(getRate.dailyPrice);
                    },
                    init: function(xml) {
                        parseXml(xml);
                    },
                }
}

现在我运行getallrates();它返回undefined为什么? 我也尝试了以下回报:

getallrates: function(source, $scope){
                    var allrates =  getRate();
                    var getdailyprice = allrates.dailyPrice;
                    console.log(getdailyprice);
                },

1 个答案:

答案 0 :(得分:1)

将代码更改为:

return {
                    getallrates: function(source, $scope){
                        return getRate(source, $scope);                            
                    },
                    init: function(xml) {
                        parseXml(xml);
                    },
                }