I am writting some javascript class code, which will accept array of ‘Steps’ objects, and will perform steps navigations.
What I am planning to do is , whenever Next() method is called, it should call a callback function before going to next step.
pseduo code
var mySteps = {[1,"Step1","javascriptCallbackStep1"],[2,"Step2","javascriptCallbackStep2"]};
var StepsManager.Next = function() {
var step = getcurrentStep();
if(step.javascriptCallbackStep1())
//go next
}
this javascriptCallbackStep1 is not getting called, any help with this little details will be greatly appreciated.
Thanks,
[Update:Answer]
I think I didnt ask it properly.
What I was trying to do was passing function name as string to javascript class property , and expecting it to be called.
passedValue = "MyFunctionName";
Class.Validate = passedValue;
And then finally for calling it was trying soemthing like below
Class.Validate();
Offcourse it was not working as in Class.Validate there was string, and there was no specification that treat it as function and not string.
So I successfully tried following
Class.Validate = function () { return window[passedValue](); };
It worked. Thanks anyways.
Javascript needs to be given a reference to a function to execute it. Passing the function itself or doing what @paul suggests is the surest way to write code that acts predictably/reliably.
You shouldn’t be passing the function name as a string, but know that you can:
Change window to the namespace where the function lives