Best practice to call functions?

Currently I have an app and I embedded node-red. I am making nodes for Classes in my app, and when I am adding a node to the flow, a new instance of the class is initialized.
What is the best practice to call the class functions?
Lets say I have a setLevel() and a setColor() function.

this.on('input', function(msg, send, done) {
		if(msg.topic=="setLevel"){

			setLevel(msg.payload)
		}
		else if(msg.topic=="setColor"){
			setColor(msg.payload)
		}
	  });

Is it viable?

I dont see anything wrong (other than you arent calling a member function e.g...

      //somehere at the top of the node-red node
      var myInstance = new MyClass()
      // later on...
      this.on('input', function(msg, send, done) {
		if(msg.topic=="setLevel"){
			myInstance.setLevel(msg.payload)
		}
		else if(msg.topic=="setColor"){
			myInstance.setColor(msg.payload)
		}
	  })

or you could hold of on initialisation until it is used,,,

      //somehere at the top of the node-red node
      var myInstance;
      // later on...
      this.on('input', function(msg, send, done) {
        if(!myInstance) myInstance = new MyClass();
		if(msg.topic=="setLevel"){
			myInstance.setLevel(msg.payload)
		}
		else if(msg.topic=="setColor"){
			myInstance.setColor(msg.payload)
		}
	  })

What is it you are concerned about?

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.