Геттер ведет себя не так, как метод - PullRequest
0 голосов
/ 13 июня 2018

Я переписываю библиотеку на TypeScript, и я изменил функцию calcPosition на геттер position.Они оба имеют почти одинаковый код:

public calcPosition (x : number, y : number, w : number, h : number) : Position {
    const colWidth = this.columnWidthInPX;
    if (this.rightToLeft) {
        return {
            right: Math.round(colWidth * x + (x + 1) * this.margin[0]),
            top: Math.round(this.rowHeight * y + (y + 1) * this.margin[1]),
            width: w === Infinity ? w : Math.round(colWidth * w + Math.max(0, w - 1) * this.margin[0]),
            height: h === Infinity ? h : Math.round(this.rowHeight * h + Math.max(0, h - 1) * this.margin[1])
        };
    } else {
        return {
            left: Math.round(colWidth * x + (x + 1) * this.margin[0]),
            top: Math.round(this.rowHeight * y + (y + 1) * this.margin[1]),
            width: w === Infinity ? w : Math.round(colWidth * w + Math.max(0, w - 1) * this.margin[0]),
            height: h === Infinity ? h : Math.round(this.rowHeight * h + Math.max(0, h - 1) * this.margin[1])
        };
    }
}

get position () : Position {
    const colWidth = this.columnWidthInPX;
    if (this.rightToLeft) {
        return {
            right: Math.round(colWidth * this.innerX + (this.innerX + 1) * this.margin[0]),
            top: Math.round(this.rowHeight * this.innerY + (this.innerY + 1) * this.margin[1]),
            width: this.innerW === Infinity ? this.innerW : Math.round(colWidth * this.innerW + Math.max(0, this.innerW - 1) * this.margin[0]),
            height: this.innerH === Infinity ? this.innerH : Math.round(this.rowHeight * this.innerH + Math.max(0, this.innerH - 1) * this.margin[1])
        };
    } else {
        return {
            left: Math.round(colWidth * this.innerX + (this.innerX + 1) * this.margin[0]),
            top: Math.round(this.rowHeight * this.innerY + (this.innerY + 1) * this.margin[1]),
            width: this.innerW === Infinity ? this.innerW : Math.round(colWidth * this.innerW + Math.max(0, this.innerW - 1) * this.margin[0]),
            height: this.innerH === Infinity ? this.innerH : Math.round(this.rowHeight * this.innerH + Math.max(0, this.innerH - 1) * this.margin[1])
        };
    }
}

Каждый раз, когда метод calcPosition вызывается из библиотеки, он вызывает его с одинаковыми аргументами:

pos = this.calcPosition(this.innerX, this.innerY, this.innerW, this.innerH);

AsВы можете видеть выше, я сделал довольно разумную вещь, преобразовав его в метод получения, который использует эти свойства напрямую, а не передает их в качестве аргументов.Обратите внимание, что эти свойства - простые типы - числа, а не объекты - поэтому я сомневаюсь, что это связано со ссылками.Тем не менее, изменяя одну строку - строку выше - на использование this.position вместо this.calcPosition(), мое приложение ведет себя по-другому. Что заставляет моего геттера вести себя не так, как мой метод?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...