1

I have an object created which belongs to particular class.

            var schCom1 = Server.CreateObject(ArchiveProgID);

Now, this object gives call to method which is dynamically decided.

            fnName += "(";
        for (counter=0;counter<fnArgs.length;counter++)
        {
            if(counter > 0)
                fnName += ",";
            fnName += fnArgs[counter];
        }
        fnName += ")";
        writeComment("Ready to call method:" + "schCom1." + fnName);
        // according to the type of recurrance, call method
        eval("schCom1."+ fnName);   

Is there any substitution possible to this eval call ?

Any Help will be valuable .

Thanks in advance.
Tazim.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Star123
  • 669
  • 2
  • 8
  • 19
  • 2
    possible duplicate of [Calling dynamic function with dynamic parameters in Javascript](http://stackoverflow.com/questions/676721/calling-dynamic-function-with-dynamic-parameters-in-javascript) – Ignacio Vazquez-Abrams Nov 12 '10 at 08:39
  • 1
    You should use `fnName+="("+fnArgs.join(",")+")"` instead of that `for` loop. – Ben Nov 12 '10 at 08:45

2 Answers2

3

Provided that fnName really is a name of a method of the schCom1 object:

schCom1[fnName].apply(schCom1,fnArgs);

Basically, the apply method for function objects allow you to call the function and provide the context (parent object) and arguments. And the apply method expects function arguments to be supplied as an array, which is useful in this case.

See the documentation of the apply method for more info:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/apply

there's also the call method which does something similar but expects function arguments as a regular argument list:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/call

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • thanx a lot . answer was really helpful for understanding the concept – Star123 Nov 12 '10 at 08:47
  • fnName is not the real name of function name of schCom1 . fnName = Request("fnName"). it should be schCom1[Request("fnName")].apply(schCom1,fnArgs); – Star123 Nov 12 '10 at 09:23
2

You can use a more simpler

schCom1[fname].apply(schCom1, fnArgs)

that would replace all your code up there

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95