Run simple script

Dec 11, 2013 at 11:37 AM
Hi, everyone!
I'm new in using ClearScript. Could you show me how I can run simple script like:

function TestMethod(testObj)
{
//some action
return testObj;
}

without using construction like: "engine.Script.TestMethod(testObj)" ?

Can I somehow pass the string script to method like: "engine.evaluate(stringScript)"? And how to pass json to that method?
Coordinator
Dec 11, 2013 at 2:05 PM
Hello xtracer!

If you have script code in a string, you can use the Evaluate() method to run it:
string code = @"
    function TestMethod(testObj) 
    { 
        //some action 
        return testObj; 
    }
";

engine.Evaluate(code);
The script code in this case defines a function called TestMethod. To execute that function, you can use additional scripts:
engine.Evaluate("TestMethod(123)");
engine.Evaluate("TestMethod({foo: 123, bar: 'abc'})");
Or, you can use the Script property to execute the function directly, without parsing or compiling additional scripts:
engine.Script.TestMethod(123);
Please let us know if you have additional questions.

Good luck!
Dec 11, 2013 at 3:32 PM
Thank you! This is what I wanted to see.