Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have one complex function. I planned to send a function into it.

function ComplexFunction( customFunction : function)
{
    //Complex things
    customFunction();
    //Complex things
}

But the function I planned to send has different signature

function FunctionA ( enumParameter : EnumX )
function FunctionB ( enumParameter : EnumY )
function FunctionC ( enumParameter : EnumZ )

So this is not an option because I don't know what type will be sending in.

function ComplexFunction( customFunction : function , enumForCustomFunction : Enum??? )

(This is Unity's javascript with #pragma strict, so I must indicate the parameter type.)

So I think about pre-apply those enum parameter, make it into parameter-less function and send that in for ComplexFunction to call. Is this possible?

share|improve this question
never worked with Unity3d, but couldn't you just wrap the real function to be called with an unnamed function? (e.g. ComplexFunction(function () { return FunctionA(EnumX); });) – Yoshi 23 mins ago

1 Answer

In plain JavaScript you can bind parameters to a function like this:

var multiply = function(x,y) { return x*y };
var doubleIt = multiply.bind(null, 2);

So calling doubleIt(3) now returns 6. (The null sets the context of the function, in this case we don't care about that, so we use null). You can read more on this topic here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind


In your case I would call ComplexFunction with FunctionA like so:

ComplexFunction(FunctionA.bind(null, AValueFromEnumX));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.