Monday, 5 June 2017

access variables in nodejs

Im an experienced coder in C# and now I´ve to deal with nodejs .. and I cannot get things to work as I expected them to work :)

I have a "class" declared like this :

var Gpio;

function Lights()
{
    [.more variables declared the same way..]
    this.o1 = "";
    this.o2 = "";
}

//then I have some methods added like this:
Lights.prototype.Init = function()
{
    var Gpio = require('pigpio').Gpio,
    //init of my variables
    o1 = new Gpio(17, {mode: Gpio.OUTPUT}),
    o2 = new Gpio(18, {mode: Gpio.OUTPUT});
}

//then I have other methods that try to use o1 and o2 
//that where already defined and initialiced

Lights.prototype.On = function(id)
{
    if(id == 1)
        o1.digitalWrite(true);
    else if(id == 2)
        o2.digitalWrite(true);
}

But when I run it I get :

o1.digitalWrite(false);
^

ReferenceError: o1 is not defined

How can I make those o1 o2 o3 accesibles by the methods ???



via javirs

No comments:

Post a Comment