Javascript函数总是返回true

Javascript function always returns true?

本文关键字:返回 true 函数 Javascript      更新时间:2023-09-26

我有这个函数它是一个对象的属性,

Layer.prototype.mouseInBounds = function() {
    // Return value
    var ret = true;
    // Layer mouse co-ordinates are not valid
    if (this.mouse.x < 0 || this.mouse.y < 0 || this.mouse.x >= this.width || this.mouse.y >= this.height) {
        // Set layer mouse co-ordinates to false
        this.mouse.x = false;
        this.mouse.y = false;
        // Set return value
        ret = false;
    }
    return ret;
};

但是当我在另一个具有图层属性的对象中这样调用它时,

this.layer.mouseInBounds() // true

无论mouseInBounds函数中ret的值是多少?

编辑

为了更好地理解我的问题mouse是层的属性,当添加

console.log(ret);

在返回语句之前,我确实得到了truefalse

总是返回true,因为:

  • this.mouse.x < 0总是false

  • this.mouse.y < 0总是false

  • this.mouse.x >= this.width总是false

  • this.mouse.y >= this.height总是false

我不知道mouse.xmouse.y具体代表什么,但如果它们是从窗口左上角测量的x/y鼠标坐标,那么我不知道它们怎么会小于0