Ich schreibe eine Prototypfunktion, die ein Datum vom Objekt nimmt und dann eine Gigasekunde (10 Milliarden Sekunden) hinzufügt. Hier ist mein Code:JavaScript: Kopieren Sie einen Objektschlüsselwert, ohne den referenzierten Objektschlüssel zu ändern
var Gigasecond = function(userDate){
this.userDate = userDate;
}
Gigasecond.prototype.date = function(){
var gigaDate = this.userDate;
var gigaSecond = Math.pow(10, 9);
console.log("This is the first console log: " + this.userDate);
//adding the billion seconds to the date
gigaDate.setFullYear(gigaDate.getFullYear() + Math.floor(gigaSecond/31536000));
gigaDate.setDate(gigaDate.getDate() + Math.floor((gigaSecond % 31536000)/86400));
gigaDate.setHours(gigaDate.getHours() + Math.floor(((gigaSecond % 31536000) % 86400)/3600));
gigaDate.setMinutes(gigaDate.getMinutes() + Math.floor((((gigaSecond % 31536000) % 86400) % 3600)/60));
gigaDate.setSeconds(gigaDate.getSeconds() + (((gigaSecond % 31536000) % 86400) % 3600) % 60);
console.log("this should equal the first console log: " + this.userDate);
}
Das eingegebene Datum in this.userDate
ist Sunday, Sep 13, 2015 18:00
. Ich möchte this.userDate
für einen anderen Teil der Funktion intakt halten. Problem ist, wenn ich gigaDate
ändere, ändert es auch this.userDate
. Hier ist die Ausgabe der Konsole:
This is the first console log: Sun Sep 13 2015 18:00:00 GMT-0600 (MDT) this should equal the first console log: Thu May 30 2047 19:46:40 GMT-0600 (MDT)
Irgendwelche Hinweise?
Das funktioniert! Ich danke dir sehr. – user5854440