N
N
nexcode2015-11-30 20:24:39
typescript
nexcode, 2015-11-30 20:24:39

Error while inheriting class (this is not a X object). How to fix?

Actually, I made this class:

class trueDate extends Date {
        constructor(date: string) {
            super(date)
        }
 
        getFixDay() {
            var day = this.getDay()
            return day === 0 ? 7 : day
        }
 
        addDate(date: number) {
            this.setTime(this.getTime() + date * 86400)
        }
    }

But, when calling methods of an inherited class (for example: trueDateObj.getDate() ) I get an error:
Uncaught TypeError: this is not a Date object.
The compiled js looks like this:
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var trueDate = (function (_super) {
    __extends(trueDate, _super);
    function trueDate(date) {
        _super.call(this, date);
    }
    trueDate.prototype.getFixDay = function () {
        var day = this.getDay();
        return day === 0 ? 7 : day;
    };
    trueDate.prototype.addDate = function (date) {
        this.setTime(this.getTime() + date * 86400);
    };
    return trueDate;
})(Date);

I create an instance like this: I
var trueDateObj = new Date("строка-с-датой")
rewrote the class like this, but I still get this error:
class trueDate extends Date {
        constructor(date: string) {
            super(date)
        }

        getDay(): number {
            var day = super.getDay()
            return day === 0 ? 7 : day
        }

        addDate(date: number) {
            super.setTime(super.getTime() + date * 86400)
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question