javascript-麻烦大家帮我看看下面的js函数有什么问题?

问题描述

麻烦大家帮我看看下面的js函数有什么问题?
function getDateBy8Week(theDate){
var dateSet = [];
theDate = new Date(theDate);

for(var i=0;i<8;i++){    alert(theDate);    dateSet.push(theDate);    theDate = new Date(theDate.setDate(theDate.getDate()-7));}alert(""haha"");for(var j=0;j<dateSet.length;j++){    alert(dateSet[j]);}return dateSet;

}
当theDate传入""2015/06/17""时,最后打印出2015/06/102015/06/032015/05/272015/05/202015/05/132015/05/062015/04/292015/04/22我明明已经将2015/06/17保存在数组中,而数组中没有保存2015/04/22,为什么会出现如下结果?

解决方案

对象存储的是地址
theDate = new Date(theDate.setDate(theDate.getDate()-7)); 你在还没有给theDate new新地址之前 已经改变了上一个地址存储的值
所以没有06/17,是这个样子咯

解决方案二:

    //theDate = new Date(theDate.setDate(theDate.getDate()-7));    theDate = new Date(theDate.getDate()-7);

在 new Data() 之前 theDate 和数组成员是同一个对象,theDate.setDate() 等于修改了数组成员的值。

解决方案三:
第一个for循环跑8次,2015/06/17~2015/04/29正好8个周,如果想输出2015/04/22
theDate = new Date(theDate.setDate(theDate.getDate()-7));
写在dateSet.push(theDate);前面就行了

时间: 2024-08-02 06:43:38

javascript-麻烦大家帮我看看下面的js函数有什么问题?的相关文章

javascript-麻烦大家帮我看看下面的js代码有什么问题?

问题描述 麻烦大家帮我看看下面的js代码有什么问题? var datas=[]; var datam=[]; for(var i=0;i<dataset.provinces.length;i++) { var name= dataset.provinces[i].name; var value= dataset.provinces[i].value; datas[name]=value; //例如 datas[北京]=14149 alert(datas.length); datam.push(v

麻烦大家帮我看看下面的sql问题?

问题描述 麻烦大家帮我看看下面的sql问题? select distinct a.* from a,b where to_char(a.reg_date,'yyyy-mm-dd') >= '2015-08-10' and to_char(a.reg_date,'yyyy-mm-dd') < '2015-08-11' and a.card_no = b.card_no and a.dept_name is not null and a.dept_name <> '.' and a.v

android-谁能帮我看看下面的Android程序错在哪里

问题描述 谁能帮我看看下面的Android程序错在哪里 解决方案 自己仔细调试下,如果提问,请给出代码.错误提示.出现什么问题等才好帮你. 解决方案二: 你根本没放code...... 解决方案三: 把你的代码放出来看看 解决方案四: 神马都没有................ 解决方案五: 好歹把代码贴出来啊....... 解决方案六: 哈哈你根本没放code...... 解决方案七: 看到有这样问问题的也是醉了......

变换-麻烦各位帮我改下程序吧,输入#include &amp;amp;quot;stdafx.h&amp;amp;quot;在VC++上面不能运行呀~~

问题描述 麻烦各位帮我改下程序吧,输入#include "stdafx.h"在VC++上面不能运行呀~~ #include ""stdafx.h""#include #include #include #include #include #define N 1000 /*定义复数类型*/ typedef struct { double real; double img; }complex; void fft(); /*快速傅里叶变换*/ void

烦请大神将下面的js代码改写为php代码(加密用)

问题描述 烦请大神将下面的js代码改写为php代码(加密用) function md5(A) { return hex_md5(A) } function md5js(pass, code, uin) { var I = hexchar2bin(md5(pass)); var H = md5(I + uin); var G = md5(H + code.toUpperCase()); return G } var hexcase = 1; var b64pad = ""; var ch

|M| 300分 SQL存储过程没有学过 麻烦大家帮我改一下存储过程让他能够不按主键排序

问题描述 下面的是我现在用的分页存储过程但这里只能按主建进行排序现在我的表有一个日期字段 我想要在下面的参数@Col为SaleDate的时候进行日期排序要怎么改谢谢SETQUOTED_IDENTIFIEROFFGOSETANSI_NULLSOFFGOALTERPROCEDURESP_Page@TBVARCHAR(1000), --表名@COLVARCHAR(1000),--主键--@COLTYPEINT,@ORDERBYBIT,--是否Desc排序@COLLISTVARCHAR(3000),--

麻烦高手帮我看下,谢谢

问题描述 privatevoidpd_PrintPage(objectsender,System.Drawing.Printing.PrintPageEventArgse){intx=e.MarginBounds.Left;inty=e.MarginBounds.Top;lines=this.richTextBox.Text.Split(param);while(linesPrinted<lines.length){e.Graphics.DrawString(lines[linesPrinted

麻烦大家帮我看下apache的错误日志,不明白哪里出错了

问题描述 系统:windows2003apache版本:2.2.21php版本:5.2.9以下为错误日志:[TueNov0812:23:442011][notice]Parent:childprocessexitedwithstatus3221225477--Restarting.[TueNov0812:23:442011][notice]Apache/2.2.21(Win32)PHP/5.2.9-2configured--resumingnormaloperations[TueNov0812:

麻烦帮我解决下 找不到存储过程

问题描述 这个是我要运行的方法publicstaticvoidAddProducts(Productsgen){stringsql="insertProducts(ProductsId,name,picture,picturetwo,Content)"+"values(@ProductsId,@name,@picture,@picturetwo,@Content)";SqlParameter[]para=newSqlParameter[]{newSqlParamet