6
There is a Date constructor (which is the date constructor that already comes in JS), and I would like to create a lastYear() method that brings the "year = year-1" (for example and learning purposes).
I know I might as well do it using the prototype
:
Date.prototype.lastYear = function(){
return this.getFullYear() - 1;
}
console.log(new Date('1900-10-10').lastYear());
However, this way I learned in the books that this is not healthy for Compiler, since you are changing the natural prototype chain of javascript.
So I’d like to understand how I could do this in the best way using classes:
class Newfunc {
constructor(){
this.Date = Date
this.lastYear();
}
lastYear(Date) {
return this.Date.getFullYear() -1
}
class Date extends Newfunc
new Date('1900-10-10').lastYear()
//expected result: '1899'
Without success, someone can help?
Yeah, I agree with everything you said. I had actually read about not using prototype, but that’s actually what you had said.
– Juliano Henrique