I'm not sure how to create an object like I would do in Java. For now I've the following code in the file CalendarDate.js and was confident to use the same instance. I'd imported this file in my main.js file and handled it like this: var selectedDay = CalendarDate.SELECTED_DAY;
But if I'm going to try to create a new object (I'm dead sure, that it couldn't work like this, thought maybe things are different javascript though) like this:
var secondDay = CalendarDate.SELECTED_DAY;
I'm using the same reference as selectedDay. Not sure, if this makes sense in javascript but, I need rather something like: var selectedDay = new CalendarDate() Maybe even something like this ->
var selectedDay = new CalendarDate("2018-10-21") //or like this:
var selectedDay = new CalendarDate(2018,10,21).
So maybe I want to use cascading constructors.
Hopefully you got my point and if somebody could give me hints how to change my CalendarDate.js file in order to use it in an expected behaviour, please feel free to answer :)
[If any beginners (like I'm one) needs something like this just swipe it :)]
//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};
Now - on @ssougnez suggestion - I've tried to solve it like this, get this error if I try to access it like var selectedDay = new CalendarDate("2018-01-01"): "TypeError: Object is not a constructor (evaluating '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;
}