1

I'm trying to assign different dates to separate task() objects; but javascript is behaving unexpectedly in this case, as can be seen from attached output for below snippet.

While assigning dates(i.e. Expected Output) it's returning intended values(same that are being assigned), but while viewing them(i.e. Actual Output), it somehow overrides value for all date variables and returns only the last assigned value for all of them.

Please help me understand how is this happening!

  

  var task = function() {
  this.name, this.date,this.id;
  this.saveTask = function(n,d)
  {
    this.name = n;
    this.date = d;
  };
  this.gettask = function()
  {
   return this.id+": "+this.date;
  };
}

var d = new Date();
var obj = [];
console.log("Expected Output"); 
for(i=0;i<5;i++){
 obj[i] = new task();
  obj[i].id = i;
  d.setDate(i);
  obj[i].date = d;
  console.log(i+": "+d); //Display assinged value
}

console.log("Actual Output");
for(i=0;i<5;i++){
 console.log(obj[i].gettask());
}

OUTPUT IN CONSOLE

 Expected Output
0: Sat Apr 30 2016 10:42:20 GMT+0530 (India Standard Time)
1: Fri Apr 01 2016 10:42:20 GMT+0530 (India Standard Time)
2: Sat Apr 02 2016 10:42:20 GMT+0530 (India Standard Time)
3: Sun Apr 03 2016 10:42:20 GMT+0530 (India Standard Time)
4: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
 Actual Output
0: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
1: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
2: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
3: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
4: Mon Apr 04 2016 10:42:20 GMT+0530 (India Standard Time)
Sachink
  • 1,425
  • 10
  • 22
  • Agree! but, I am assigning the the diffrent date every time to the `task.date` – Sachink May 05 '16 at 05:48
  • 2
    No you aren't. `var d = new Date();` assigns a date to *d*. `obj[i].date = d` assigns a **reference** to the date, it doesn't copy it. ;-) – RobG May 05 '16 at 06:19

1 Answers1

2

You're assigning the same date object to each task. Create a new date for each one in the loop.

for (let i = 0; i < 5; i++) {
    let d = new Date();
    d.setDate(i);

    let t = new task();
    t.date = d;
    // etc

    obj.push(t);
}

I suggest you have a read of this ~ Is JavaScript a pass-by-reference or pass-by-value language?


Note that the time portion of each date object may be different due to execution time. If you want them all to have the same time but different dates, you'll need to make a slight change.

Community
  • 1
  • 1
Phil
  • 157,677
  • 23
  • 242
  • 245
  • thanks I knew this solution, but I can't understand why is this happening; even I am assigning different dates for each object but it shows the same value. – Sachink May 05 '16 at 05:56
  • @Sachink they aren't different, they are the exact same `Date` instance, assigned by reference. – Phil May 05 '16 at 05:57
  • Got it! The date object assigned by reference. I thought it's making copy while assigning to the variable. – Sachink May 05 '16 at 06:35