I have a situation when I set expire to a date object. After altering 'expire' the 'object.created' is changed when it should not be. Why is 'object.created' be changed? Thanks!
let expire = object.created; // object.created: Wed Mar 02 2016
expire.setDate(12);
console.log(expire); // Wed Mar 12 2016
console.log(object.created); //Wed Mar 12 2016 <-- WHY?!
Assigning a reference to an object from one place to another does not involve making a copy. If you want a complete copy of the date:
let expire = new Date(object.created);
If you don't do that, there's only one Date instance involved; both expire and object.created refer to the same object in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With