我正在尝试使用循环来动态生成事件侦听器 在节点中。但是我无法正确定位按钮对象。 我使用了-global-和-this-但两者都返回错误。
var Gpio = require('onoff').Gpio,
button1 = new Gpio(17, 'in', 'both'),
button2 = new Gpio(27, 'in', 'both'),
button3 = new Gpio(22, 'in', 'both'),
button4 = new Gpio(23, 'in', 'both');
var i = 5;
while(i-->1)
{
console.log(i);
global["button"+i].watch(function(err, value) {
if (err) exit();
x++;
console.log("button "+i+" has been pressed" );
})}
如何正确指向while循环内的对象?
由于