【问题标题】:Give A same value as B without making them interconnected [duplicate]给 A 与 B 相同的值而不使它们相互连接[重复]
【发布时间】:2019-03-06 19:39:57
【问题描述】:

我正在处理日期,我的代码如下所示:

(currentDate 只是要记住的实际日期,showedDate 是一个变量,它在函数中更改以从我的数据库中获取特定于日期的数据

var currentDate = new Date();
var shownDate = currentDate;

现在,如果我更改显示日期(例如添加 7 天),当前日期也会更改。我知道分配这样的变量会使 shownDate 类似于指向 currentDate 从而改变它。

如果您需要更多代码或其他任何内容,请告诉我。

【问题讨论】:

  • 只需从currentDate 创建一个新日期并将shownDate 设置为该日期

标签: javascript variables


【解决方案1】:

您可以使用new Date进行克隆:

var currentDate = new Date();
var shownDate = new Date(currentDate);

【讨论】:

  • 感谢您的快速帮助,我会尽快接受答案:)
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 2017-02-19
  • 2022-12-11
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多