【问题标题】:Javascript every() method for class object类对象的 Javascript every() 方法
【发布时间】:2021-03-04 01:47:38
【问题描述】:

我已经做了一个格子是javascript。我想用 every() 方法检查我的鼠标是否在它们上。我的班级有一个内置的悬停功能,但我不知道如何实现该方法。

class area {

    constructor(x, y, w, h) {
        this.x = x
        this.y = y
        this.w = w
        this.h = h
        this.posx = width - (x + w)
        this.posy = height / row_num

    }

    hover(mx, my) {
        if (mx > this.x && mx < this.x + this.w && my > this.y && my < this.y + this.h) {
            return true
        }
    }
    
    
    for (let i = 0; i < hovergrid.length; i++) {
        

        if (hovergrid[i].hover(mouseX, mouseY)) {
  }



    const isValid = hovergrid.every(item => item.hover(mouseX, mouseY) === true)
    console.log({ isValid })
&lt;img id="displayedimage" onerror="this.onerror=null; this.style.display = 'none'" src="images/1.png"&gt;

这是我尝试过的方法,但效果不佳。有人可以给我一些提示吗?非常感谢`

   const isValid = hovergrid.every(item => item.hover(mouseX, mouseY) === true)
   console.log({ isValid })

`

【问题讨论】:

    标签: javascript html arrays object


    【解决方案1】:

    根据您的代码

    hover(mx, my) {
            if (mx > this.x && mx < this.x + this.w && my > this.y && my < this.y + this.h) {
                return true
            }
    

    这仅返回 true 而不是 false。 every() 函数期望布尔返回。但在此函数中,如果为 false,则不会返回任何内容。 因此,我相信在包含该部分之后“every()”应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2021-11-30
      • 2015-04-23
      • 2018-11-24
      • 2018-03-20
      • 1970-01-01
      相关资源
      最近更新 更多