Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript日期有两个不同的值,具体取决于打印日期的上下文_Javascript_Angularjs - Fatal编程技术网

Javascript日期有两个不同的值,具体取决于打印日期的上下文

Javascript日期有两个不同的值,具体取决于打印日期的上下文,javascript,angularjs,Javascript,Angularjs,我看到了一些奇怪的现象,日期对象作为对象的一部分输出时只有一个值,而不是它自己输出时。下面的代码片段加上输出可能最好地解释了我遇到的问题。特别感兴趣的是$scope.firstDateOfWeek $scope.month = $scope.month - 1; $scope.firstDateOfMonth = new Date($scope.year, $scope.month); $scope.firstDay = $scope.firstDateOfMonth.getDay(); $sc

我看到了一些奇怪的现象,日期对象作为对象的一部分输出时只有一个值,而不是它自己输出时。下面的代码片段加上输出可能最好地解释了我遇到的问题。特别感兴趣的是
$scope.firstDateOfWeek

$scope.month = $scope.month - 1;
$scope.firstDateOfMonth = new Date($scope.year, $scope.month);
$scope.firstDay = $scope.firstDateOfMonth.getDay();
$scope.firstDateOfWeek = new Date($scope.year, $scope.month, 2 - $scope.firstDay);
$scope.lastDate = new Date(new Date($scope.year, $scope.month + 1) - 1);

console.log($scope);
console.log($scope.firstDateOfWeek);
console.log($scope);
console.log($scope.firstDateOfWeek);
console.log($scope);
console.log($scope.firstDateOfWeek);
输出(相同x 3):

具体来说,当我执行console.log($scope)时,然后$scope.firstDateOfWeek会打印为2015-02-02,但当我显式打印变量console.log($scope.firstDateOfWeek)时,它会打印为2014-12-29

有人知道为什么会这样吗

编辑:在Hacketo的评论之后,下面是代码的其余部分

var dates = [];

currentDate = $scope.firstDateOfWeek;
for (i = 0;
     (currentDate <= $scope.lastDate ||
     (currentDate > $scope.lastDate && currentDate.getDay() != 1)); i++) {

    if (currentDate.getMonth() == $scope.month) {
        dates.push(new Date(currentDate));

    } else {
        dates.push(null);
    }
    currentDate.setDate(currentDate.getDate() + 1);
}
var日期=[];
currentDate=$scope.firstDateOfWeek;
对于(i=0;
(currentDate$scope.lastDate&¤tDate.getDay()!=1);(i++){
if(currentDate.getMonth()=$scope.month){
日期。推送(新日期(当前日期));
}否则{
日期。推送(空);
}
currentDate.setDate(currentDate.getDate()+1);
}
发生的情况是currentDate持有对$scope.firstDateOfWeek的引用,该引用正在for循环中更新


剩下的唯一问号是console.log()显示的异步行为。有人能解释一下吗?

这可能是控制台保留的引用,firstDateOfWeek在之后被修改,控制台更新$scope对象属性(但不是直接打印的,因为它返回字符串:$scope.firstDateOfWeek)。在你的代码之后,firstDateOfWeek还有其他的修改吗?你真的给出了你所有的代码吗?如果是,请在另一个浏览器中尝试。正确。更新问题。