【问题标题】:JavaScript: Copy an object key value without changing the referenced object keyJavaScript:复制对象键值而不更改引用的对象键
【发布时间】:2016-08-01 01:38:18
【问题描述】:

我正在编写一个原型函数,该函数从对象中获取日期,然后向其添加千兆秒(100 亿秒)。这是我的代码:

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)

有什么提示吗?

【问题讨论】:

    标签: javascript object prototype


    【解决方案1】:

    您想创建一个新的日期对象,而不是简单地将一个对象分配给另一个对象。

    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);
    }
    

    现在为我输出以下内容:

    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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多