2016-12-25 29 views
0

在我的代碼中,我有一個名爲「ToDoItem」的對象, 而我有另一個名爲「ToDoList」的對象。 應該有一個'ToDoItem'對象的數組。對象列表受到某些別名損壞

編輯 - 一個改變運行能夠代碼:

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

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

 
\t } 
 
\t if(!(date instanceof Date)) 
 
\t \t return("please enter A valid Date"); 
 
\t 
 
\t date.setUTCMonth(date.getUTCMonth()-1) 
 
\t console.log(date.getUTCMonth()); 
 
\t this.Text = Text; \t \t \t //type of String 
 
\t this.date = date; \t \t \t //type of Date 
 
\t this.isDone = isDone; \t \t //type of boolean 
 
\t 
 
\t if(typeof this.postpone != "function") 
 
\t { 
 
\t \t ToDoItem.prototype.toString = function() 
 
\t \t { 
 
\t \t \t if (typeof(this) == "undefined") 
 
\t \t \t { 
 
\t \t \t alert("here"); 
 

 
\t \t \t \t return; 
 
\t \t \t } 
 
\t \t \t return ("Task: " + this.Text + ", Date:" + this.date + " , isDone: " + this.isDone); 
 
\t \t } 
 
\t \t 
 
\t \t ToDoItem.prototype.isOutOfDate = function(now) 
 
\t \t { 
 
\t \t \t if(now == undefined || !(now instanceof Date)) 
 
\t \t \t \t \t return "please enter Date"; 
 
\t \t \t \t 
 
\t \t \t if(now < this.date) \t \t \t 
 
\t \t \t \t return true; 
 
\t \t \t return false; 
 
\t \t } 
 

 
\t \t ToDoItem.prototype.postpone = function(days) 
 
\t \t { 
 
\t \t \t if(days == undefined || !(typeof(days) == "number")) 
 
\t \t \t \t return "please enter a number"; 
 
\t \t \t if(days < 0) 
 
\t \t \t \t return "this is negative number"; 
 
\t \t \t if(days >= 0) 
 
\t \t \t { 
 
\t \t \t \t var oldDay = this.date.getDate(); 
 
\t \t \t \t this.date.setDate(oldDay + days); 
 
\t \t \t } 
 
\t \t } \t 
 
\t } 
 
} 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
function ToDoList() 
 
{ 
 
\t var List =new Array; 
 
\t this.List = List; 
 
\t if(typeof this.addItem != "function") 
 
\t { 
 
\t \t ToDoList.prototype.constructor = ToDoList; 
 
\t \t ToDoList.prototype.toString = function() 
 
\t \t { 
 
\t \t \t var str = ""; 
 
\t \t \t for(var i=0;i<List.length;i++) 
 
\t \t \t \t str += i+1 + ":" + List[i].toString() + "\n"; \t \t \t 
 
\t \t \t return str; 
 
\t \t } 
 
\t \t 
 
\t \t ToDoList.prototype.addItem = function(item) 
 
\t \t { 
 
\t \t \t if(item == undefined || (!(item instanceof ToDoItem))) 
 
\t \t \t \t return "please enter object type of item"; 
 
\t \t \t 
 
\t \t \t this.List[List.length] = item; 
 
\t \t } 
 
\t \t 
 
\t \t ToDoList.prototype.getForDate = function(date) 
 
\t \t { 
 
\t \t \t if(date == undefined || !(date instanceof Date)) 
 
\t \t \t \t return "please enter Date"; 
 
\t \t \t 
 
\t \t \t var arr = []; 
 
\t \t \t var capacity = 0; 
 
\t \t \t 
 
\t \t \t for(var i=0;i<List.length;i++) 
 
\t \t \t \t if((List[i].date.getDate() == date.getDate()) && (List[i].date.getMonth() == date.getMonth()) && (List[i].date.getYear() == date.getYear())) 
 
\t \t \t \t \t arr[capacity++] = List[i]; 
 
\t \t \t \t 
 
\t \t \t 
 
\t \t \t return arr; 
 
\t \t } 
 
\t \t 
 
\t \t ToDoList.prototype.getOutOfDate = function() 
 
\t \t { 
 
\t \t \t var arr = []; 
 
\t \t \t var capacity = 0; 
 

 
\t \t \t for(var i=0;i<List.length;i++) 
 
\t \t \t \t if(List[i].isOutOfDate(new Date()) == true) 
 
\t \t \t \t \t arr[capacity++] = item; 
 
\t \t \t return arr; 
 
\t \t } 
 
\t \t 
 
\t \t ToDoList.prototype.postpone = function(index, days) 
 
\t \t { 
 
\t \t \t if(index == undefined || days == undefined || typeof(index) != Number || typeof(days) != Number) 
 
\t \t \t \t return "enter Numbers"; 
 
\t \t \t if(days < 0 || index < 0) 
 
\t \t \t \t return "enter valid numbers"; \t \t 
 
\t \t \t if(index >= this.List.length) 
 
\t \t \t \t return "enter valid index"; \t \t \t 
 
\t \t \t 
 
\t \t \t List[i].postpone(days); 
 
\t \t } 
 
\t } 
 
} 
 

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

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

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

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

 
    <head> 
 
    </head> 
 
    
 
    <body> 
 
     <script>tester();</script> 
 
    </body> 
 
</html>
我已經添加了整個相關的代碼。需要在瀏覽器控制檯上運行它。

問題是,當我生成兩個不同的'ToDoList'對象並打印時,我在第二個對象上得到第一個數據,依此類推。

感謝您的任何幫助。

+0

歡迎的StackOverflow!我查看了你的代碼,但是其中的一些內容從這個問題中漏掉了,特別是'ToDoItem'的代碼。您可以使用代碼片段功能將代碼轉換爲可運行的示例,幷包含使其運行所需的所有代碼?如果編輯問題並使用'<>'按鈕,它會在問題中插入一個可運行的片段。 –

+0

感謝您的評論。 –

回答

0

我檢查了您的代碼,並發現了一些問題:

  • 列表,而不是this.list
  • ==,而不是===
  • typeof(val)而不是(typeof val)。 TYPEOF是運營商,而不是一個函數
  • 錯位的原型以下

請查看代碼

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

 
\t if ((typeof Text !== "string") || (Text.length < 1)) \t { 
 
\t \t return undefined; 
 
\t } 
 
\t if(!(date instanceof Date)) 
 
\t \t return("please enter A valid Date"); 
 

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

 
ToDoItem.prototype.toString = function() { 
 
\t return ("Task: " + this.Text + ", Date:" + this.date + " , isDone: " + this.isDone); 
 
} 
 

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

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

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

 

 

 
function ToDoList() { 
 
\t this.List = []; 
 
} 
 

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

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

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

 
ToDoList.prototype.getOutOfDate = function() { 
 
\t var arr = []; 
 
\t for(var i=0; i<this.List.length; i++) 
 
\t \t if (this.List[i].isOutOfDate(new Date()) === true) 
 
\t \t \t arr.push(this.List[i]); 
 
\t return arr; 
 
} 
 

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

 
\t this.List[index].postpone(days); 
 
} 
 

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

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

 
\t var l1 = new ToDoList(); 
 
\t l1.addItem(t1); 
 
\t l1.addItem(t2); 
 
\t console.log(l1.toString()); 
 
\t var l2 = new ToDoList(); 
 
\t l2.addItem(t3); 
 
\t l2.addItem(t4); 
 
\t console.log(l2.toString()); 
 
} 
 

 
tester();

0

我認爲問題在於在所有ToDoList函數中使用List值而不是this.List。

在你的情況下,在創建類ToDoList的第一個實例期間,你聲明瞭使用範圍值List的函數。

所有下一個對象都使用相同的實現,所以它們使用來自第一個實例的List。

1

1)更換所有List(關閉VAR)與this.List(對象ptoperty)

2)將所有原型的方法進行構造 - solution