我不确定如何像在Java中那样创建对象。现在,我在CalendarDate.js文件中具有以下代码,并且有信心使用相同的实例。我将此文件导入了main.js文件,并按以下方式进行处理: var selectedDay = CalendarDate.SELECTED_DAY;
但是,如果我要尝试创建一个新对象(我很确定,它不能像这样工作,虽然可能是JavaScript有所不同),如下所示:
var secondDay = CalendarDate.SELECTED_DAY;
我正在使用与selectedDay相同的参考。不知道,如果这在javascript中有意义,但是,我需要一些类似的东西: var selectedDay = new CalendarDate() 甚至是这样的->
var selectedDay = new CalendarDate("2018-10-21") //or like this:
var selectedDay = new CalendarDate(2018,10,21).
所以也许我想使用级联构造函数。
希望您能理解我的观点,如果有人可以给我提示如何更改CalendarDate.js文件以便以预期的方式使用它,请随时回答:)
[如果有任何初学者(例如我是一个人)需要这样的东西,只需擦一下它即可:)]
//CalendarDate.js
var SELECTED_DAY = {
dateString: "",
day: "",
month: "",
year: "",
statusStr: "",
'setDate': function(dateStr){
this.dateString = dateStr;
if(this.hasRightFormat()){
this.day = this.dateString.split("-")[2];
this.month = this.dateString.split("-")[1];
this.year = this.dateString.split("-")[0];
}else{
this.dateString = "";
this.day = "";
this.month = "";
this.year = "";
}
},
'hasRightFormat': function(){
this.statusStr = "";
if(this.dateString !== "" || this.dateString !== "undefined" ){
this.statusStr += "it's not undefined (Y)\n";
if(this.dateString.split("-").length == 3){
this.statusStr+= "It has a split-length of 3 (Y)\n";
if(this.dateString.split("-")[0].length != 4){
this.statusStr+= "WRONGYEARFORMAT? (N)\n";
return false;
}
if(this.dateString.split("-")[1].length != 2 || this.dateString.split("-")[2].length != 2){
this.statusStr+= "IT HAS EITHER A WRONG DAY OR MONTH FORMAT\n";
return false;
}
//right format
return true;
}
}
// return false;
return false;
},
'getStatus': function(){
return this.statusStr;
},
'test': function(){
return "TESTVALUE";
},
'day': function(){
return this.date.split("-")[2];
/*if(this.hasRightFormat()){
return this.date.split("-")[2];
}
return "undefined";*/
},
'month': function(){
if(this.hasRightFormat()){
return this.date.split("-")[1];
}
return null;
},
'year': function(){
if(this.hasRightFormat()){
return this.date.split("-")[0];
}
return null;
},
};
module.exports = {SELECTED_DAY};
现在-关于@ssougnez的建议-我已经尝试解决此问题,如果我尝试像var selectedDay = new CalendarDate(“ 2018-01-01”)那样访问它,则会出现此错误:“ TypeError:Object is不是构造函数(评估'new _CalendarDate2.default()')“
function CalendarDate(dateStr){
this.dateString = dateStr;
this.day = this.day();
this.month = this.month();
this.year = this.year();
}
this.hasRightFormat = function(){
this.statusStr = "";
if(this.dateString !== "" || this.dateString !== "undefined" ){
this.statusStr += "it's not undefined (Y)\n";
if(this.dateString.split("-").length == 3){
this.statusStr+= "It has a split-length of 3 (Y)\n";
if(this.dateString.split("-")[0].length != 4){
this.statusStr+= "WRONGYEARFORMAT? (N)\n";
return false;
}
if(this.dateString.split("-")[1].length != 2 || this.dateString.split("-")[2].length != 2){
this.statusStr+= "IT HAS EITHER A WRONG DAY OR MONTH FORMAT\n";
return false;
}
//right format
return true;
}
}
// return false;
return false;
}
this.setDate = function(dateString){
cache = this.dateString;
this.dateString = dateString;
if (this.hasRightFormat()){
this.day = this.day();
this.month = this.month();
this.year = this.year();
}
}
this.day = function(){
return this.dateString.split("-")[2];
if(this.hasRightFormat()){
return this.date.split("-")[2];
}
return null;
}
this.month = function(){
if(this.hasRightFormat()){
return this.dateString.split("-")[1];
}
return null;
}
this.year = function(){
if(this.hasRightFormat()){
return this.dateString.split("-")[0];
}
return null;
}