Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSbScript
LOCAL oJs
LOCAL oResult
LOCAL oFuncCall
LOCAL oArg
LOCAL oArgsArray
LOCAL oSbOut

nSuccess := 0

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

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

oSbScript := CreateObject("Chilkat.StringBuilder")
oSbScript:Append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }")

oJs := CreateObject("Chilkat.Js")

oResult := CreateObject("Chilkat.JsonObject")
oResult:EmitCompact := 0

//  Call Eval to add the function to the context's global object
nSuccess := oJs:Eval(oSbScript, oResult)
IF (nSuccess == 0)
    //  Examine the result for an exception.
    ? oResult:Emit()

    //  Also examine the LastErrorText.
    ? oJs:LastErrorText
    oSbScript:destroy()
    oJs:destroy()
    oResult:destroy()
    RETURN
ENDIF

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

oFuncCall := CreateObject("Chilkat.JsonObject")
oFuncCall:EmitCompact := 0

//  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
//      }
//    ]
//  }

oFuncCall:UpdateString("name", "describeCar")

//  Create the JSON object that is the argument.
oArg := CreateObject("Chilkat.JsonObject")
oArg:UpdateString("make", "Toyota")
oArg:UpdateString("model", "Corolla")
oArg:UpdateInt("year", 2022)

//  Create the arguments array.
oArgsArray := CreateObject("Chilkat.JsonArray")
oArgsArray:AddObjectCopyAt(0, oArg)

//  Add the "args" array to the funcCall.
oFuncCall:AppendArrayCopy("args", oArgsArray)

? oFuncCall:Emit()

nSuccess := oJs:CallFunction(oFuncCall, oResult)
IF (nSuccess == 0)
    //  Examine the result for an exception.
    ? oResult:Emit()

    //  Also examine the LastErrorText.
    ? oJs:LastErrorText
    oSbScript:destroy()
    oJs:destroy()
    oResult:destroy()
    oFuncCall:destroy()
    oArg:destroy()
    oArgsArray:destroy()
    RETURN
ENDIF

? oResult:Emit()

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

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

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

oSbOut := CreateObject("Chilkat.StringBuilder")
oJs:ConsoleOutputSb(oSbOut)
? oSbOut: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:

oFuncCall:Clear()
oFuncCall:UpdateString("name", "describeCar")
oFuncCall:UpdateString("args[0].make", "Toyota")
oFuncCall:UpdateString("args[0].model", "Corolla")
oFuncCall:UpdateInt("args[0].year", 2022)
? oFuncCall:Emit()

oSbScript:destroy()
oJs:destroy()
oResult:destroy()
oFuncCall:destroy()
oArg:destroy()
oArgsArray:destroy()
oSbOut:destroy()