繁体   English   中英

从 JavaScript 中的箭头 function 返回数组

[英]Returning array from arrow function in JavaScript

我正在 p5.js 的 JavaScript 中制作太阳能系统发电机,我想从箭头 function 返回一组 rgb 值,但它不起作用。 星星是白色的,而不是黄色、橙色或红色。

class Star {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.size = random(50, 70);
    this.color = () => {
      let colorChoice = floor(random(6));
      switch(colorChoice) {
        case 0: case 1: case 2: case 3:
          return [255, 255, 0];
          break;
        case 4:
          return [255, 150, 0];
          break;
        case 5:
          return [255, 0, 0];
          break;
      }
    }
  }

  show() {
    noStroke();
    fill(this.color[0], this.color[1], this.color[2]);
    circle(this.x, this.y, this.size);
  }
}

function 本身或其他地方有问题吗?

this.color - 是 function。 尝试将您的show()方法更新为

let color = this.color();
fill(color[0], color[1], color[2]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM