9
How to determine which value javascript should use for logical operations on objects?
I am creating a competency object that needs the following functionalities:
- Encapsulates the logic of creating a competence: month/year in MM/YYYY format
- When concatenated with string it returns the MM/YYYY format
- Two Competency objects can be compared
Functions str_pad() and checkdate() were used from the library php.js
My problem is comparing two objects. Since I have two variables data1 and data2, i would like to compare them to know which one is larger or if the two are of equal competence, using the logical operators of javascript directly between the variables.
ex: data1 > data2; data1 == data2;
I have tried overwriting the prototype.value but it did not work.
Competencia = function(competencia){
var date = this.dateFromCompetencia(competencia);
Object.defineProperty(this, 'date', {
value: date
});
};
Competencia.prototype.toString = function () {
return this.formatado;
};
Object.defineProperties(Competencia.prototype, {
formatado: {
get: function(){
return this.competencia.replace(/^(\d{2})(\d{4})$/,"$1/$2");
}
},
competencia: {
get: function(){
return this.mes + this.ano;
}
},
mes: {
get: function(){
return mes = str_pad(String(this.date.getMonth()+1), 2, '0', 'STR_PAD_LEFT');
}
},
ano: {
get: function(){
return ano = String(this.date.getFullYear());
}
},
proxima:{
value: function(){
var mes = str_pad(String(Number(this.mes)+1), 2, '0', 'STR_PAD_LEFT');
var comp= mes + this.ano;
console.log(comp);
return new Competencia(comp);
}
},
dateFromCompetencia: {
value: function (competencia) {
competencia = String(competencia).replace(/\D/g, '');;
var mes = competencia.substr(-6,2);
var ano = competencia.substr(-4);
if(!checkdate(mes, 01, ano))
throw new Error('Competencia incorreta');
return new Date(ano, Number(mes)-1);
}
}
});
// função str_pad e checkdate do site phpjs.org
function str_pad(e,t,n,r){var i="",s;var o=function(e,t){var n="",r;while(n.length<t){n+=e}n=n.substr(0,t);return n};e+="";n=n!==undefined?n:" ";if(r!=="STR_PAD_LEFT"&&r!=="STR_PAD_RIGHT"&&r!=="STR_PAD_BOTH"){r="STR_PAD_RIGHT"}if((s=t-e.length)>0){if(r==="STR_PAD_LEFT"){e=o(n,s)+e}else if(r==="STR_PAD_RIGHT"){e=e+o(n,s)}else if(r==="STR_PAD_BOTH"){i=o(n,Math.ceil(s/2));e=i+e+i;e=e.substr(0,t)}}return e}
function checkdate(e,t,n){return e>0&&e<13&&n>0&&n<32768&&t>0&&t<=(new Date(n,e,0)).getDate()}
try{
fev14 = new Competencia('02/2014');
jan15 = new Competencia('01/2015');
fev2014 = new Competencia('02/2014');
document.getElementById('comparacao1').innerHTML = fev14 > jan15;
document.getElementById('comparacao2').innerHTML = fev14 > fev2014;
}catch(e){
alert(e.message);
}
Comparação <b>fev14 > jan15</b> -
<b>Resultado</b>: <span id="comparacao1"></span>
<b>Esperado</b>: false;
<br>
<br>
Comparação <b>fev14 == fev2014</b> -
<b>Resultado</b>: <span id="comparacao2"></span>
<b>Esperado</b>: true;
Very good. This gave me an idea that I always wanted to apply regarding Objects of Value. If you allow me I will edit your answer to my solution. Thank you.
– Paulo Henrique
Glad you could help! About the editing, it depends. If it’s going to change a lot what I wrote, maybe it’s better to post it as a separate answer (and you can even accept your own answer if you want).
– bfavaretto
Check it out. What do you think?
– Paulo Henrique
If you’re just going to add one or two lines of code, ok @Paulohenrique. Otherwise, best post as response.
– bfavaretto
@Paulohenrique Now that I saw that you suggested an edition. It was rejected by 2 users, because it was very big, it changed my answer a lot. I really recommend posting as a separate response.
– bfavaretto