对象列表被某些别名

时间:2016-12-25 08:03:09

标签: javascript oop

在我的代码中,我有一个名为“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”对象并打印时,我会在第二个对象上获得第一个对象的数据,依此类推。

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

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

2)将所有prototyped方法移出构造函数 - solution

答案 1 :(得分:0)

我认为问题在于在所有ToDoList函数中使用List值而不是this.List。

在您的情况下,在创建类ToDoList的第一个实例时,您将声明使用范围值List的函数。

所有下一个对象都使用相同的实现,因此它们使用第一个实例中的List。

答案 2 :(得分:0)

我查看了您的代码并发现了一些问题:

  • 列出而不是this.list
  • ==而不是===
  • typeof(val)而不是(typeof val)。 Typeof是一个运算符,而不是一个函数
  • 错放原型

请参阅此后的代码

'use strict';
function ToDoItem(Text, date, isDone = false) {

	if ((typeof Text !== "string") || (Text.length < 1))	{
		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
}

ToDoItem.prototype.toString = function() {
	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() {
	this.List = [];
}

ToDoList.prototype.constructor = ToDoList;
ToDoList.prototype.toString = function() {
	var str = "";
	for(var i=0; i<this.List.length; i++)
		str += i+1 + ":" + this.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.push(item);
}

ToDoList.prototype.getForDate = function(date) {
	if((date === undefined) || !(date instanceof Date))
		return "please enter Date";
	var arr = [];
	for(var i=0; i<this.List.length; i++)
		if( (this.List[i].date.getDate() === date.getDate()) && (this.List[i].date.getMonth() === date.getMonth()) && (this.List[i].date.getYear() === date.getYear()) )
			arr.push(this.List[i]);
	return arr;
}

ToDoList.prototype.getOutOfDate = function() {
	var arr = [];
	for(var i=0; i<this.List.length; i++)
		if (this.List[i].isOutOfDate(new Date()) === true)
			arr.push(this.List[i]);
	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(! this.List[index])
		return "enter valid index";

	this.List[index].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());
}

tester();