在对象方法中通过外部方法设置变量

时间:2015-07-14 08:00:05

标签: javascript object methods

我正在使用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

1 个答案:

答案 0 :(得分:1)

function getFormattedDate(),您有

var result = input.replace(pattern, function(match,p1,p2,p3) {... return date; });

因此result包含replace函数的返回值,但getFormattedDate不返回任何内容==&gt;从CreateReview调用时未定义。

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