Function as parameter

This sounds more like a javascript or typescript question, but I was able to do it in other environments but not in ZapWorks. I want to do this:

function toggle(...args: Function[]) {
    args.forEach((arg) => {arg() === true ? arg(false) : arg(true)});
}

toggle(node1.enabled, node2.visible);

I want to pass a bunch of functions that returns false or true with no arguments, and if it’s true it calls the function with false and vice versa. I know I could do this with states or just do something more specific like this:

function toggle() {
  node1.enabled() === true? node1.enabled(false):node1.enabled(true);
  etc
}

But really it’s not the way I want.

1 Like

Bump! Anybody knows how to do this?

a(console.log);//this works!
b(parent.position);//this doesn’t T_T. Tried with ‘any’ too
function a(myfunction: Function) {
myfunction(“bla”);//works as console.log(“bla”), as expected
}
function b(myfunction: Function) {
myfunction([1,2,3]);//does nothing apparently. I want it to work as if I typed parent.position([1,2,3])
}

Another simpler example:

let b = console.log;
b(“this works”);
let a = parent.position;
a([1,2,3]);//this doesn’t

Maybe I’m missing something, but I can always make it work on repl.it. What zapworks does different, and how to change so it works?

var a = {
b: () => {
console.log(“asdasd”);
}
}

function c(d) {
d();
}

c(a.b);

EDIT
Problem solved. I needed to bind the method to the parent: parent.position.bind(parent)