What you need to do is build another room with another white cat instead, so when you change that cat for a black one later, it will not influence the white cat in the first room. If you later access the room through the other door (d1), then you're just going to find the black cat, of course. If you call d2.changeCat(black), then you're entering the room through the door d2 and changing the white cat for a black one. When you say d2 = d1, you just build another door to the same room with the same white cat. Imagine there's a room with one door (d1) and inside the room there's a white cat. What you need to do is build another object with the same content: date3 = new Date(date1.getTime()) So when you change it using tDate() or tDate() doesn't matter as well. Whether you access it viaĭate1.toDateString() or date3.toDateString() doesn't matter. But there's still just one object with one content (e.g. Let variable date3 point at the same object as variable date1. To put it simply, Date is an object and "Fri Jul 04 2014" is only its contents. If you're interested in memory management in JavaScript, I suggest you have a look at this: I hope it is clearer with this little explanation. The 2nd and 3rd allocations consist in creating new Date objects, containing a copy of the object in date1. In this example, there are three memory allocations for 3 different objects. Var date3 = new Date(date1) // Memory allocation n☃ Var date2 = new Date(date1) // Memory allocation n☂ Example 2: var date1 = new Date() // Memory allocation for an object Note: The object that was originally in date2 still exists, but is not referenced anymore (it will be garbage-collected). In this example, there are two memory allocations but only one of them is useful as the object in date2 is not used. Example: In the following web document setDate() method sets the day of the month to 23 of a given. dayValue : An integer from 1 to 31, representing the day of the month. Var date3 // Obviously no memory allocation hereĭate3 = date1 // No memory allocation either, date2 and date3ĭate2 = date1 // become references of the object in date1 The setDate() method is used to set the day of the month for a given date according to local time. Var date2 = new Date(1991,4,11) // Memory allocation n☂ Example 1: var date1 = new Date() // Memory allocation for an object Because: date.getDate() returns the day of the month of the date variable tDate(int) sets the day of date (just the day, leaving the year and month intact) What the statement tDate(act. Otherwise, your variables all point to the same Date object (initially referenced by date1). The statement tDate(act.getDate()+1) does not make moreDay the date act plus one day. You need to make a copy of your dates, like this: date3 = new Date(date1.getTime())
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |