Sample code for 30+ languages & platforms
Java Requires Chilkat v11.4.0+

Call a JavaScript Function Passing an Object Argument

See more JavaScript Examples

Demonstrates how to call a JavaScript function with an argument that is an object.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Js;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This is the JavaScript function we'll call:

        // function describeCar(car) {
        //     console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
        // }

        StringBuilder sbScript = new StringBuilder();
        sbScript.append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }");

        Js js = new Js();

        JsonObject result = new JsonObject();
        result.setEmitCompact(false);

        // Call Eval to add the function to the context's global object
        try {
            js.eval(sbScript, result);
        } catch (ChilkatException e) {
            // Examine the result for an exception.
            System.out.println(result.emit());

            // Also examine the LastErrorText.
            System.out.println(e.getLastErrorText());
            return;
        }

        // ------------------------------------------------------------------------------
        // Call the function describeCar(car)

        JsonObject funcCall = new JsonObject();
        funcCall.setEmitCompact(false);

        // Create JSON specifying the function name and arguments
        // In this case, there is only 1 argument, and it is an object.

        // {
        //   "name": "describeCar",
        //   "args": [
        //     {
        //       "make": "Toyota",
        //       "model": "Corolla",
        //       "year": 2022
        //     }
        //   ]
        // }

        funcCall.updateString("name", "describeCar");

        // Create the JSON object that is the argument.
        JsonObject arg = new JsonObject();
        arg.updateString("make", "Toyota");
        arg.updateString("model", "Corolla");
        arg.updateInt("year", 2022);

        // Create the arguments array.
        JsonArray argsArray = new JsonArray();
        argsArray.addObjectCopyAt(0, arg);

        // Add the "args" array to the funcCall.
        funcCall.appendArrayCopy("args", argsArray);

        System.out.println(funcCall.emit());

        try {
            js.callFunction(funcCall, result);
        } catch (ChilkatException e) {
            // Examine the result for an exception.
            System.out.println(result.emit());

            // Also examine the LastErrorText.
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(result.emit());

        // The describeCar JavaScript function returns nothing.
        // Therefore, the result is "undefined".

        // {
        //   "type": "undefined",
        //   "value": "undefined"
        // }

        // However, the function emitted text to the console.

        StringBuilder sbOut = new StringBuilder();
        js.consoleOutputSb(sbOut);
        System.out.println(sbOut.getAsString());

        // Output:
        // This is a 2022 Toyota Corolla.

        // -----------------------------------------------------------
        // Note: If the object argument is simple, this is an alternative
        // and simpler way of creating the funcCall:

        funcCall.clear();
        funcCall.updateString("name", "describeCar");
        funcCall.updateString("args[0].make", "Toyota");
        funcCall.updateString("args[0].model", "Corolla");
        funcCall.updateInt("args[0].year", 2022);
        System.out.println(funcCall.emit());
    }
}