第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問名稱為字符串的對象

訪問名稱為字符串的對象

qq_笑_17 2022-06-05 09:49:18
我有許多儀表聲明如下:var g1 = new RadialGauge({    renderTo: 'gauge1',    width: 300,    height: 300,    units: 'Grados',    title: "Temp_d9",    valueBox: true,    animationRule: 'bounce',    animationDuration: 500}).draw();名稱是 g1..g2..g3..g10..如果我想改變儀表的值,我這樣做:g1.value = 10;我想要做的是在 for 循環(huán)中更改所有儀表的值.. 我嘗試過的,當然沒有工作:for(var i = 1; i <= 10 ; i ++){    var name = "t" + i;    name.value = lst_val;}我怎樣才能做到這一點?這是可能的?謝謝
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

如果您的儀表變量t1,...,t10被定義為全局變量,那么您可以window['t'+i]for循環(huán)中處理它們i。

但是@Helder Sepulveda 建議將它們保存在一個通用的數(shù)組對象 ( gauges) 中,因為這樣可以避免名稱沖突。


查看完整回答
反對 回復(fù) 2022-06-05
?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

您應(yīng)該使用數(shù)組:


gauges = []

gauges.push(new RadialGauge(...))

gauges.push(new RadialGauge(...))

gauges.push(new RadialGauge(...))

gauges.forEach(g => g.draw());


//change the values later

gauges.forEach(g => {

    var name = "t" + i;

    ...

    g.value = 123;

});

這是一個完整的工作示例,說明它的外觀:


class RadialGauge {

  constructor(ctx, x, y, color) {

    this.ctx = ctx;

    this.color = color;

    this.x = x; this.y = y;

    this.value = 0

  }

  

  draw() {

    this.ctx.beginPath();

    this.ctx.fillStyle = this.color;

    this.ctx.arc(this.x, this.y, 22, 0, 2 * Math.PI);

    this.ctx.fill();

    this.ctx.beginPath();

    this.ctx.moveTo(this.x, this.y)

    var x = this.x + Math.sin(this.value/10) * 20;

    var y = this.y + Math.cos(this.value/10) * 20;

    this.ctx.lineTo(x, y)

    this.ctx.stroke();

  }

}


const canvas = document.getElementById('c');

const ctx = canvas.getContext('2d');


gauges = []

gauges.push(new RadialGauge(ctx, 50, 50, "red"))

gauges.push(new RadialGauge(ctx, 150, 50, "blue"))

gauges.push(new RadialGauge(ctx, 100, 100, "green"))

gauges.forEach(g => g.draw());


function reDraw() {

  ctx.clearRect(0,0,400,400)

  gauges.forEach(g => {  

    g.value++

    g.draw()

  });

}

setInterval(reDraw, 20);

<canvas id="c" width=400 height=150></canvas>


查看完整回答
反對 回復(fù) 2022-06-05
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號