在对象的方法中通过外部方法设置变量
Setting variable through external method in method of object
我使用javascript对象的方法来创建HTML来编写该对象。在该方法中,我有一个日期(以SQL date的字符串格式),我将其格式化为在外部方法中添加MMM YYYY。外部方法可以很好地返回我需要的字符串,但是当我在对象的方法中设置变量时,它返回为undefined
。
相应代码:
function CreateReview(reviewID, visitDate) {
var reviewObject = {
iD: reviewID,
visitDate: visitDate,
CreateReviewObject : function(c) {
var reviewContainer = document.createElement('div');
reviewContainer.id = 'Review_' + this.iD;
reviewContainer.className = 'Card Review';
var headerDIV = document.createElement('div');
headerDIV.className = 'Header';
var dateTagsDIV = document.createElement('div');
dateTagsDIV.className = 'DateTags';
var datesDIV = document.createElement('div');
datesDIV.className = 'Dates';
var formattedVisitDate = getFormattedDate(this.visitDate);
console.log(formattedVisitDate);
var dateDIV = document.createElement('div');
dateDIV.className = 'Date';
dateDIV.innerHTML = formattedVisitDate;
datesDIV.appendChild(dateDIV);
dateTagsDIV.appendChild(datesDIV);
headerDIV.appendChild(dateTagsDIV);
reviewContainer.appendChild(headerDIV);
return reviewContainer;
}
};
return reviewObject;
}
function getFormattedDate(input) {
input = input.replace(/-/g,'/');
var pattern = /(.*?)'/(.*?)'/(.*?)$/;
var result = input.replace(pattern,function(match,p1,p2,p3){
p2 = parseInt(p2);
p3 = parseInt(p3);
var months = ['jan','feb','maa','apr','mei','jun','jul','aug','sep','okt','nov','dec'];
var date = (p3<10?"0"+p3:p3) + " " + months[parseInt(p2-1)] + " " + p1;
console.log(date);
return date;
});
}
getFormattedDate
中控制台的输出则为
12 mei 2015
而在CreateReview
中为
undefined
我也试过下面的方法:
function CreateReview(reviewID, restaurant, kitchenTypes, tags, pictures, ratings, thumbPicture, visitDate, introduction, description) {
var reviewObject = {
iD: reviewID,
visitDate: visitDate,
CreateReviewObject : function(c) {
var getFormattedVisitDate = function(visitDate) {
return function() { getFormattedDate(visitDate); };
};
var reviewContainer = document.createElement('div');
reviewContainer.id = 'Review_' + this.iD;
reviewContainer.className = 'Card Review';
var headerDIV = document.createElement('div');
headerDIV.className = 'Header';
var dateTagsDIV = document.createElement('div');
dateTagsDIV.className = 'DateTags';
var datesDIV = document.createElement('div');
datesDIV.className = 'Dates';
var formattedVisitDate = getFormattedVisitDate(this.visitDate);
console.log(formattedVisitDate);
var dateDIV = document.createElement('div');
dateDIV.className = 'Date';
dateDIV.innerHTML = formattedVisitDate;
datesDIV.appendChild(dateDIV);
dateTagsDIV.appendChild(datesDIV);
headerDIV.appendChild(dateTagsDIV);
reviewContainer.appendChild(headerDIV);
return reviewContainer;
}
};
return reviewObject;
}
function getFormattedDate(input) {
input = input.replace(/-/g,'/');
var pattern = /(.*?)'/(.*?)'/(.*?)$/;
var result = input.replace(pattern,function(match,p1,p2,p3){
p2 = parseInt(p2);
p3 = parseInt(p3);
var months = ['jan','feb','maa','apr','mei','jun','jul','aug','sep','okt','nov','dec'];
var date = (p3<10?"0"+p3:p3) + " " + months[parseInt(p2-1)] + " " + p1;
console.log(date);
return date;
});
}
这给了我在CreateReview
中的输出:
return function() { getFormattedDate(visitDate); };
为什么CreateReview
调用返回undefined
,而控制台不返回?
在您的function getFormattedDate()
中,您有
var result = input.replace(pattern, function(match,p1,p2,p3) {... return date; });
所以result
包含replace
函数的返回值,但getFormattedDate
在从CreateReview
调用时不返回任何值==> undefined
在函数getFormattedDate
的末尾添加return result;
。
相关文章:
- 如何从窗体调用外部javascript方法
- 有什么方法可以调用外部URI方案吗
- GWT:可以从外部JavaScript而不是JSNI调用Java方法吗
- 什么'这是使用jQuery将一些文本锚定到外部链接的最简单方法
- 将参数传递到外部 JS 脚本的最佳方法
- 将$ionicLoading.show()用于外部链接的方法
- AngularJS-从Angular外部触发摘要的最佳方法
- angularjs模态将数据传输到外部控制器的正确方法
- 您可以从方法外部识别事件的调用方吗
- 从外部Javascript文件调用方法
- 是否有任何方法可以访问外部范围's变量
- 将外部Javascript文件注入html页面并在其上执行方法WP8 Webbrowser
- 多重继承或访问对象外部的属性和方法
- 当我将javascript代码放在外部站点.js文件中时,Ajax页面方法不起作用
- 在方法外部使用 javascript 变量 Value form
- 无法从函数或方法外部返回
- 无法从函数或方法外部返回
- 试图访问控制器方法外部注入的依赖项
- 不能从函数或方法外部返回
- 反应:从渲染方法外部更新组件的 props,而不使用状态