gpt4 book ai didi

javascript - 对象列表因某些别名而损坏

转载 作者:行者123 更新时间:2023-12-03 05:22:23 25 4
gpt4 key购买 nike

在我的代码中,我有一个名为“ToDoItem”的对象,我有另一个名为“ToDoList”的对象。它应该有一个“ToDoItem”对象数组。

编辑 - 更改为可运行代码:

function ToDoItem(Text, date, isDone = false)
{

if (!(typeof(Text) == "string") || (Text.localeCompare("") == 0))
{
return undefined;

}
if(!(date instanceof Date))
return("please enter A valid Date");

date.setUTCMonth(date.getUTCMonth()-1)
console.log(date.getUTCMonth());
this.Text = Text; //type of String
this.date = date; //type of Date
this.isDone = isDone; //type of boolean

if(typeof this.postpone != "function")
{
ToDoItem.prototype.toString = function()
{
if ( typeof(this) == "undefined")
{
alert("here");

return;
}
return ("Task: " + this.Text + ", Date:" + this.date + " , isDone: " + this.isDone);
}

ToDoItem.prototype.isOutOfDate = function(now)
{
if(now == undefined || !(now instanceof Date) )
return "please enter Date";

if(now < this.date)
return true;
return false;
}

ToDoItem.prototype.postpone = function(days)
{
if(days == undefined || !(typeof(days) == "number"))
return "please enter a number";
if(days < 0)
return "this is negative number";
if(days >= 0)
{
var oldDay = this.date.getDate();
this.date.setDate(oldDay + days);
}
}
}
}





function ToDoList()
{
var List =new Array;
this.List = List;
if(typeof this.addItem != "function")
{
ToDoList.prototype.constructor = ToDoList;
ToDoList.prototype.toString = function()
{
var str = "";
for(var i=0;i<List.length;i++)
str += i+1 + ":" + List[i].toString() + "\n";
return str;
}

ToDoList.prototype.addItem = function(item)
{
if(item == undefined || (!(item instanceof ToDoItem)) )
return "please enter object type of item";

this.List[List.length] = item;
}

ToDoList.prototype.getForDate = function(date)
{
if(date == undefined || !(date instanceof Date))
return "please enter Date";

var arr = [];
var capacity = 0;

for(var i=0;i<List.length;i++)
if( (List[i].date.getDate() == date.getDate()) && (List[i].date.getMonth() == date.getMonth()) && (List[i].date.getYear() == date.getYear()) )
arr[capacity++] = List[i];


return arr;
}

ToDoList.prototype.getOutOfDate = function()
{
var arr = [];
var capacity = 0;

for(var i=0;i<List.length;i++)
if( List[i].isOutOfDate(new Date()) == true)
arr[capacity++] = item;
return arr;
}

ToDoList.prototype.postpone = function(index, days)
{
if(index == undefined || days == undefined || typeof(index) != Number || typeof(days) != Number)
return "enter Numbers";
if(days < 0 || index < 0)
return "enter valid numbers";
if(index >= this.List.length)
return "enter valid index";

List[i].postpone(days);
}
}
}

function tester()
{
var d1 = new Date(2015, 1, 1);
var d2 = new Date(2015, 12, 1);
var d3 = new Date(2016, 10, 17);
var d4 = new Date(2016, 2, 14);

var t1 = new ToDoItem("clean the house", d1);
var t2 = new ToDoItem("fix toilet", d2);
var t3 = new ToDoItem("buy shampoo", d3);
var t4 = new ToDoItem("call dad", d4);

var l1 = new ToDoList();
l1.addItem(t1);
l1.addItem(t2);
console.log(l1.toString());
var l2 = new ToDoList();
l2.addItem(t3);
l2.addItem(t4);
console.log(l2.toString());
}
<!DOCTYPE html>

<html>
<script type = "text/javascript" src="ToDo.js">
</script>

<head>
</head>

<body>
<script>tester();</script>
</body>
</html>
我已经添加了完整的相关代码。需要在浏览器控制台上运行它。

问题是,当我生成两个不同的“ToDoList”对象并打印时,我在第二个对象上获取第一个对象的数据,依此类推。

感谢您的帮助。

最佳答案

1) 将所有List(闭包变量)替换为this.List(object-ptoperty)

2) 将所有原型(prototype)方法移出构造函数 - solution

关于javascript - 对象列表因某些别名而损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com