从嵌套函数返回的对象? [英] Return objects from nested functions?
本文介绍了从嵌套函数返回的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图返回一个函数内的对象作为另一个函数内的对象:
函数(){
// 身体...
VAR dailyPrice = NULL;
功能FUN1(XML){
/***功能**/
} 功能FUN2(XML){
/*功能***/
}
功能的getRate(来源$范围){
VAR DATEVALUE = $(日期,源)的.text()|| ;
如果(!DATEVALUE){
返回null;
}
VAR dailyPrice = $(DailyPrice源)的.text()|| ;
VAR weeklyPrice = $(WeeklyPrice源)的.text()|| ;
VAR monthlyPrice = $(MonthlyPrice源)的.text()|| ;
变种isAvailable = $(IsAvailable源)的.text()===1;
VAR minimumStay数=($(MinimumStay源)的.text());
如果(isNaN(minimumStay)){
minimumStay = DEFAULT_MINIMUM_STAY;
} 返回{
日期:新的Date(DATEVALUE)
dailyPrice:dailyPrice,
weeklyPrice:weeklyPrice,
monthlyPrice:monthlyPrice,
版权所有:isAvailable,
minimumStay:minimumStay
}; }
返回{
getallrates:功能(源,$范围){
的getRate(源,$范围);
的console.log(getRate.dailyPrice);
},
初始化:功能(XML){
parseXml(XML);
},
}
}
现在当我运行getallrates();它返回undefined为什么?
我也曾尝试以下回报:
getallrates:功能(源,$范围){
变种allrates =的getRate();
VAR getdailyprice = allrates.dailyPrice;
的console.log(getdailyprice);
},
解决方案
更改code这样:
{回报
getallrates:功能(源,$范围){
返回的getRate(来源$范围);
},
初始化:功能(XML){
parseXml(XML);
},
}
I am trying to return objects inside of a function as an object inside of another function:
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);
},
}
}
now when i run getallrates(); it returns undefined why?? I have also tried the following returns:
getallrates: function(source, $scope){
var allrates = getRate();
var getdailyprice = allrates.dailyPrice;
console.log(getdailyprice);
},
解决方案
Change the code to this:
return {
getallrates: function(source, $scope){
return getRate(source, $scope);
},
init: function(xml) {
parseXml(xml);
},
}
这篇关于从嵌套函数返回的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文