Javascript 为什么数组中的所有日期元素都是相同的日期?

Javascript 为什么数组中的所有日期元素都是相同的日期?,javascript,while-loop,Javascript,While Loop,我对Javascript有一个非常奇怪的问题。我试图循环查看我的日期,以进行一些检查并为数组添加值,但当我返回数组时,它会显示所有具有最后一个值的集合。下面是我的代码: function myFunction() { var todayDate = new Date(); var firstDay = new Date(todayDate.getFullYear(), todayDate.getMonth(), 1); var lastDay = new Date(tod

我对Javascript有一个非常奇怪的问题。我试图循环查看我的日期,以进行一些检查并为数组添加值,但当我返回数组时,它会显示所有具有最后一个值的集合。下面是我的代码:

function myFunction() {
    var todayDate = new Date();
    var firstDay = new Date(todayDate.getFullYear(), todayDate.getMonth(), 1);
    var lastDay = new Date(todayDate.getFullYear(), todayDate.getMonth() + 1, 0);
    var testDates=[];

        while (firstDay <= lastDay) {
            var currentDate = firstDay;

            testDates.push( firstDay);

            firstDay.setDate(firstDay.getDate() + 1);
        }

    document.getElementById("demo").innerHTML = testDates;
}
函数myFunction(){
var todayDate=新日期();
var firstDay=新日期(todayDate.getFullYear(),todayDate.getMonth(),1);
var lastDay=新日期(todayDate.getFullYear(),todayDate.getMonth()+1,0);
var testDates=[];

while(firstDay您不是将日期添加到数组中,而是对日期的引用。然后,如果您更新
firstDay
,则会更新数组中所有元素的日期。(因为它们都指向同一日期)。请尝试像这样克隆日期:

function myFunction() {
    var todayDate = new Date();
    var firstDay = new Date(todayDate.getFullYear(), todayDate.getMonth(), 1);
    var lastDay = new Date(todayDate.getFullYear(), todayDate.getMonth() + 1, 0);
    var testDates=[];

    while (firstDay <= lastDay) {
        var currentDate = firstDay;

        testDates.push( new Date(firstDay.getTime()) );

        firstDay.setDate(firstDay.getDate() + 1);
    }

    document.getElementById("demo").innerHTML = testDates;
}
函数myFunction(){
var todayDate=新日期();
var firstDay=新日期(todayDate.getFullYear(),todayDate.getMonth(),1);
var lastDay=新日期(todayDate.getFullYear(),todayDate.getMonth()+1,0);
var testDates=[];

while(firstDay您不是将日期添加到数组中,而是对日期的引用。然后,如果您更新
firstDay
,则会更新数组中所有元素的日期。(因为它们都指向同一日期)。请尝试像这样克隆日期:

function myFunction() {
    var todayDate = new Date();
    var firstDay = new Date(todayDate.getFullYear(), todayDate.getMonth(), 1);
    var lastDay = new Date(todayDate.getFullYear(), todayDate.getMonth() + 1, 0);
    var testDates=[];

    while (firstDay <= lastDay) {
        var currentDate = firstDay;

        testDates.push( new Date(firstDay.getTime()) );

        firstDay.setDate(firstDay.getDate() + 1);
    }

    document.getElementById("demo").innerHTML = testDates;
}
函数myFunction(){
var todayDate=新日期();
var firstDay=新日期(todayDate.getFullYear(),todayDate.getMonth(),1);
var lastDay=新日期(todayDate.getFullYear(),todayDate.getMonth()+1,0);
var testDates=[];

而(第一天对此评论做一点修改:

函数myFunction(){
//使用变量定义模式
var todayDate=新日期(),
firstDay=新日期(todayDate.getFullYear(),todayDate.getMonth(),1),
lastDay=新日期(todayDate.getFullYear(),todayDate.getMonth()+1,0),
currentDate=第一天,
testDates=[];
//在循环的比较部分将firstDay更改为currentDate

而(currentDate对此评论做了一些更改:

函数myFunction(){
//使用变量定义模式
var todayDate=新日期(),
firstDay=新日期(todayDate.getFullYear(),todayDate.getMonth(),1),
lastDay=新日期(todayDate.getFullYear(),todayDate.getMonth()+1,0),
currentDate=第一天,
testDates=[];
//在循环的比较部分将firstDay更改为currentDate

while(currentDate)您需要创建新日期,而不是更新同一对象并多次推送。您需要创建新日期,而不是更新同一对象并多次推送。我看到了我的错误。感谢您指出我的愚蠢错误。我看到了我的错误。感谢您指出我愚蠢的错误。