我正在编写一个原型函数,它从对象中获取一个日期,然后向它添加一个gigasecond(100亿秒)。这里是我的代码:JavaScript:在不改变引用对象键的情况下复制对象键值
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);
}
日期进入this.userDate
是Sunday, Sep 13, 2015 18:00
。我想保留this.userDate
完整的功能的另一部分。问题是,当我更改gigaDate
时,它也会更改this.userDate
。这里是控制台输出:
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)
任何提示?
这个作品!非常感谢。 – user5854440