Javascript对象值无意中发生了变化

时间:2014-03-30 17:29:57

标签: javascript angularjs

我在AngularJS有以下工厂

app.factory('dateProvider', function(){
    var weekday = new Array(7);
    weekday[0]  = "Sunday";
    weekday[1]  = "Monday";
    weekday[2]  = "Tuesday";
    weekday[3]  = "Wednesday";
    weekday[4]  = "Thursday";
    weekday[5]  = "Friday";
    weekday[6]  = "Saturday";

    var monthString = new Array(12);
    monthString[0]  = "January";
    monthString[1]  = "February";
    monthString[2]  = "March";
    monthString[3]  = "April";
    monthString[4]  = "May";
    monthString[5]  = "June";
    monthString[6]  = "July";
    monthString[7]  = "August";
    monthString[8]  = "September";
    monthString[9]  = "October";
    monthString[10] = "November";
    monthString[11] = "December";

    var factory = {};
    factory.date = function(date) {
        var obj = {};
        obj.date        = date;                                             // Full date
        obj.day         = date.getDate();                                   // Current day (e.g 28)
        obj.weekday     = date.getDay() + 1;                                // Current numberical weekday (e.g 3 - i.e. Tuesday). Sunday is 1
        obj.weekdayName = weekday[date.getDay()];                           // Current weekday (e.g Tuesday)
        obj.month       = date.getMonth()+1;                                // Current numberical month (e.g 4)
        obj.monthName   = monthString[date.getMonth()];                     // Current month (e.g. January)
        obj.year        = date.getFullYear();                               // Current year (e.g. 2014)
        obj.numDays     = new Date(obj.year, obj.month, 0).getDate();       // Number of days in month

        // Return the month with the current date set as the first day of month
        obj.first = function() {    
            return factory.date(new Date(obj.year, obj.month - 1, 1));
        };
        // Return the month with the current date set as the last day of month
        obj.last  = function() {
            return factory.date(new Date(obj.year, obj.month - 1, obj.numDays));
        }
        return obj;
    }

    // Return the information for the current month
    factory.current = function() { 
        return factory.date(new Date());
    }

    // Get the nth previous month since "date" with "num" (default 1)
    factory.prev = function(date, num) {
        num = typeof num != 'undefined' ? num : 1;
        date.setMonth( date.getMonth() - num, 1);
        return factory.date(date);
    }

    // Get the nth next month since "date" with "num" (default 1)
    factory.next = function(date, num) {
        num = typeof num != 'undefined' ? num : 1;
        date.setMonth( date.getMonth() + num, 1 );
        return factory.date(date);
    }

    return factory;
});

我现在将其用作var current = dateProvider.current();然后查找上个月,我使用var prev = dateProvider.prev( current.date )

但是,current的值也会更改为上一个日期!为什么呢?

1 个答案:

答案 0 :(得分:0)

问题出在current函数:

date.setMonth( date.getMonth() - num, 1)

其中date是传入的“current.date”Date对象。因此该语句会导致副作用到现有/同一对象,也称为“current.date”


一个简单的修复如下所示(虽然我怀疑在处理TZ信息时它更复杂):

var prev = new Date(+date)                // create NEW Date object
prev.setMonth( prev.getMonth() - num, 1)  // modify NEW Date object
return factory.date(prev)                 // use NEW Date object