2013-08-04 259 views
-3

我試圖返回一個函數的內部對象作爲對象的另一個函數中:從嵌套函數返回對象?

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); 
       }, 
+0

看起來像你缺少的'返回的getRate(源,$範圍)' –

回答

1

更改代碼這樣:

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

OK了'return'所以我如何得到每日價格從這個父母的功能? – vimes1984

+0

你正在從getRate函數返回它,所以它應該像調用一樣簡單:'var rates = getAllRates(source,$ scope); console.log(rates.dailyPrice);' – Tomer

+0

這仍然沒有返回,我認爲這是因爲我試圖從angularJS中的模塊控制器中的服務中檢索它,這會把我拉上牆。 – vimes1984