2

JScript allows funky features like .apply(), whereby you can invoke a method by name, passing an array of arguments to invoke the method with. However this applies to JScript (javascript) objects, and obviously won't work on a COM component - but this is the feature I am looking for.

Is there a way to invoke a COM object method, by name, and with dynamic args from JScript? I couldn't find anything.

Thanks!

0

2 Answers 2

1

Dynamically build the script you want to call as a string then invoke it using eval(). eg eval("someObj.method()") or eval(objName + "." + methodName + "(" + args.join(",") +")").

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. Main issue with eval() is that I can't pass in a dynamic number of arguments. methodOne() may take one argument while methodTwo() takes two arguments.
@Troy - eval takes a string so you can build it dynamically. I added an example.
Sorry... my response was not clear :( although you can build the string, there is no reflection available to know how many args to pass.
0

Since I'm using WSC (Windows Script Component) I have made a workaround whereby I generate a wrapper script using XSL, transforming the input WSC (which is an XML file). The XSL generates a switch{} statement with a case for each method, supplying the correct number of args for each method.

Not quite what I was after, but I get better error checking :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.