我有一段代码比较两个日期,如果它们相同,则将布尔值设置为true。我在网上遇到错误:var nextFolioArrive = new Date(this.folios[i+1].folioDepart);
TypeError: Cannot read property "folioDepart" from undefined. (line 50, file "propertyObject")
但是,当我定义它时,它不会引发任何错误:
var folioDepart = new Date(this.folios[i].folioDepart);
我想知道我是否不能像这样的数组中“向前看”,不确定为什么会是一个问题。
property.prototype.isTurn = function(){
for (var i=0;i<this.folios.length;i++){
var folioArrive = new Date(this.folios[i].folioArrive);
var folioDepart = new Date(this.folios[i].folioDepart);
var nextFolioArrive = new Date(this.folios[i+1].folioDepart);
if(folioDepart == nextFolioArrive && folioArrive == TODAY){
Logger.log("turn day: " +this.turnDay);
return this.turnDay = true;
}
Logger.log("turn day: false" );
}
}
这是属性对象中的所有代码,我觉得自己在犯一个愚蠢的语法错误,或者看起来太小了。
var TODAY = new Date();
TODAY.setHours(0,0,0,0)
var propertyList = new Array();
function property(name,address) {
//Default variables
//Object variables
this.name = name;
this.address = address;
this.workOrders = new Array();
this.folios = new Array();
this.GIH = false;
this.turnDay = false;
propertyList.push(this);
property.prototype.listArrivals = function(){
Logger.log(this.name);
for(var i=0;i<this.folios.length;i++){
Logger.log(this.folios[i].folioID);
}
}
property.prototype.listWorkorders = function(){
Logger.log(this.name);
for(var i=0;i<this.workOrders.length;i++){
Logger.log(this.workOrders[i].workorderID);
}
}
property.prototype.getGIH = function(){
for (var i = 0;i<this.folios.length;i++){
var folioArrive = new Date(this.folios[i].folioArrive);
var folioDepart = new Date(this.folios[i].folioDepart);
if(TODAY >= folioArrive && TODAY <= folioDepart && this.GIH == false){
return this.GIH = true;
}
}
}
property.prototype.isTurn = function(){
for (var i=0;i<this.folios.length;i++){
var folioArrive = new Date(this.folios[i].folioArrive);
var folioDepart = new Date(this.folios[i].folioDepart);
var k = i+1;
var nextFolioArrive = new Date(this.folios[k].folioDepart);
if(folioDepart == nextFolioArrive && folioArrive == TODAY){
Logger.log("turn day: " +this.turnDay);
return this.turnDay = true;
}
Logger.log("turn day: false" );
}
}
}
我应该包括我的测试环境。
function myFunction() {
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
var tempPropertyList = [];
var tempWorkOrderList = [];
var tempFolioList = [];
var i;
var j;
var k;
var property1 = new property("property1","123 street");
var property2 = new property("property2","123 ave");
// folio(ID,type,propName,address,arrive,depart)
var folio1 = new folio("303245","type","property1","123 street","03/14/2019", "03/30/2019");
var folio2 = new folio("303243","type","property2","123 ave","03/26/2019", "03/30/2019");
var folio3 = new folio("303244","type","property1","123 street","03/30/2019", "04/10/2019");
var folio4 = new folio("303246","type","property2","123 ave","03/20/2019", "03/25/2019");
var folio5 = new folio("303247","type","property1","123 street","03/01/2019", "03/14/2019");
//workOrder(ID,propName,type,description,due,notes) {
var wo1 = new workOrder("12","property1","Maint","workOrder1","03/14/2019","");
var wo2 = new workOrder("13","property2","Maint","workOrder2","03/20/2019","");
var wo3 = new workOrder("14","property1","Maint","workOrder3","03/19/2019","");
var wo4 = new workOrder("15","property1","Maint","workOrder4","02/02/2019","");
var wo5 = new workOrder("16","property2","Maint","workOrder5","03/14/2019","");
var wo6 = new workOrder("17","property2","Maint","workOrder6","03/14/2019","");
var wo7 = new workOrder("18","property1","Maint","workOrder7","03/15/2019","");
var wo8 = new workOrder("19","property1","Maint","workOrder8","03/16/2019","");
var wo9 = new workOrder("10","property2","Maint","workOrder9","04/01/2019","");
var wo10 = new workOrder("11","property2","Maint","workOrder10","05/05/2019","");
//push objects to arrays
tempWorkOrderList.push(wo1,wo2,wo3,wo4,wo5,wo6,wo7,wo8,wo9,wo10);
tempFolioList.push(folio1,folio5,folio2,folio3,folio4);
tempPropertyList.push(property1,property2);
for (i=0;i<tempPropertyList.length;i++){
tempPropertyList[i].isTurn();
for (j=0;j<tempWorkOrderList.length;j++){
if (tempWorkOrderList[j].workorderProperty == tempPropertyList[i].name){
tempPropertyList[i].workOrders.push(tempWorkOrderList[j]);
}
}
for (k=0;k<tempFolioList.length;k++){
if(tempFolioList[k].folioProperty == tempPropertyList[i].name){
tempPropertyList[i].folios.push(tempFolioList[k]);
}
}
tempPropertyList[i].isTurn();
}
}
答案 0 :(得分:0)
在使用对象的属性之前,请先检查对象的存在性
property.prototype.isTurn = function(){
for (var i=0;i<this.folios.length;i++){
var folioArrive = new Date(this.folios[i].folioArrive);
var folioDepart = new Date(this.folios[i].folioDepart);
// check next folio
if (typeof this.folios[i+1] != 'undefined') {
var nextFolioArrive = new Date(this.folios[i+1].folioDepart);
if(folioDepart == nextFolioArrive && folioArrive == TODAY){
Logger.log("turn day: " +this.turnDay);
return this.turnDay = true;
}
Logger.log("turn day: false" );
}
}
}