Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsW.h>
#include <CkJsonObjectW.h>
#include <CkJsonArrayW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This is the JavaScript function we'll call:

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

    CkStringBuilderW sbScript;
    sbScript.Append(L"function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }");

    CkJsW js;

    CkJsonObjectW result;
    result.put_EmitCompact(false);

    //  Call Eval to add the function to the context's global object
    success = js.Eval(sbScript,result);
    if (success == false) {
        //  Examine the result for an exception.
        wprintf(L"%s\n",result.emit());

        //  Also examine the LastErrorText.
        wprintf(L"%s\n",js.lastErrorText());
        return;
    }

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

    CkJsonObjectW funcCall;
    funcCall.put_EmitCompact(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(L"name",L"describeCar");

    //  Create the JSON object that is the argument.
    CkJsonObjectW arg;
    arg.UpdateString(L"make",L"Toyota");
    arg.UpdateString(L"model",L"Corolla");
    arg.UpdateInt(L"year",2022);

    //  Create the arguments array.
    CkJsonArrayW argsArray;
    argsArray.AddObjectCopyAt(0,arg);

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

    wprintf(L"%s\n",funcCall.emit());

    success = js.CallFunction(funcCall,result);
    if (success == false) {
        //  Examine the result for an exception.
        wprintf(L"%s\n",result.emit());

        //  Also examine the LastErrorText.
        wprintf(L"%s\n",js.lastErrorText());
        return;
    }

    wprintf(L"%s\n",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.

    CkStringBuilderW sbOut;
    js.ConsoleOutputSb(sbOut);
    wprintf(L"%s\n",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(L"name",L"describeCar");
    funcCall.UpdateString(L"args[0].make",L"Toyota");
    funcCall.UpdateString(L"args[0].model",L"Corolla");
    funcCall.UpdateInt(L"args[0].year",2022);
    wprintf(L"%s\n",funcCall.emit());
    }