2016-08-01 37 views
2

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?

Antwort

1

Sie möchten ein neues Datumsobjekt erstellen und nicht einfach einem anderen zuweisen.

var Gigasecond = function(userDate){ 
    this.userDate = userDate; 
} 

Gigasecond.prototype.date = function(){ 
    var gigaDate = new Date(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); 
    console.log("this should equal the new date gigaDate: " + gigaDate); 
} 

Diese jetzt gibt das folgende für mich:

This is the first console log: Mon Aug 01 2016 11:43:57 GMT+1000 (AUS Eastern Standard Time)

this should equal the first console log: Mon Aug 01 2016 11:43:57 GMT+1000 (AUS Eastern Standard Time)

this should equal the new date gigaDate: Thu Apr 16 2048 13:30:37 GMT+1000 (AUS Eastern Standard Time)

+0

Das funktioniert! Ich danke dir sehr. – user5854440