gpt4 book ai didi

javascript - 通过对象方法中的外部方法设置变量

转载 作者:行者123 更新时间:2023-11-28 00:05:16 25 4
gpt4 key购买 nike

我正在使用 javascript 对象的方法来创建 HTML 来编写该对象。在该方法中,我有一个日期(采用字符串格式作为 SQL 日期),我在外部方法中将其格式化为 dd 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 的返回值功能,但是 getFormattedDateCreateReview 调用时不返回任何内容 ==> undefined .

添加return result;在函数末尾getFormattedDate .

关于javascript - 通过对象方法中的外部方法设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com