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

Delphi DLL
var
success: Boolean;
sbScript: HCkStringBuilder;
js: HCkJs;
result: HCkJsonObject;
funcCall: HCkJsonObject;
arg: HCkJsonObject;
argsArray: HCkJsonArray;
sbOut: HCkStringBuilder;

begin
success := False;

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

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

sbScript := CkStringBuilder_Create();
CkStringBuilder_Append(sbScript,'function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }');

js := CkJs_Create();

result := CkJsonObject_Create();
CkJsonObject_putEmitCompact(result,False);

//  Call Eval to add the function to the context's global object
success := CkJs_Eval(js,sbScript,result);
if (success = False) then
  begin
    //  Examine the result for an exception.
    Memo1.Lines.Add(CkJsonObject__emit(result));

    //  Also examine the LastErrorText.
    Memo1.Lines.Add(CkJs__lastErrorText(js));
    Exit;
  end;

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

funcCall := CkJsonObject_Create();
CkJsonObject_putEmitCompact(funcCall,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
//      }
//    ]
//  }

CkJsonObject_UpdateString(funcCall,'name','describeCar');

//  Create the JSON object that is the argument.
arg := CkJsonObject_Create();
CkJsonObject_UpdateString(arg,'make','Toyota');
CkJsonObject_UpdateString(arg,'model','Corolla');
CkJsonObject_UpdateInt(arg,'year',2022);

//  Create the arguments array.
argsArray := CkJsonArray_Create();
CkJsonArray_AddObjectCopyAt(argsArray,0,arg);

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

Memo1.Lines.Add(CkJsonObject__emit(funcCall));

success := CkJs_CallFunction(js,funcCall,result);
if (success = False) then
  begin
    //  Examine the result for an exception.
    Memo1.Lines.Add(CkJsonObject__emit(result));

    //  Also examine the LastErrorText.
    Memo1.Lines.Add(CkJs__lastErrorText(js));
    Exit;
  end;

Memo1.Lines.Add(CkJsonObject__emit(result));

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

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

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

sbOut := CkStringBuilder_Create();
CkJs_ConsoleOutputSb(js,sbOut);
Memo1.Lines.Add(CkStringBuilder__getAsString(sbOut));

//  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:

CkJsonObject_Clear(funcCall);
CkJsonObject_UpdateString(funcCall,'name','describeCar');
CkJsonObject_UpdateString(funcCall,'args[0].make','Toyota');
CkJsonObject_UpdateString(funcCall,'args[0].model','Corolla');
CkJsonObject_UpdateInt(funcCall,'args[0].year',2022);
Memo1.Lines.Add(CkJsonObject__emit(funcCall));

CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);
CkJsonObject_Dispose(arg);
CkJsonArray_Dispose(argsArray);
CkStringBuilder_Dispose(sbOut);