![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Call a JavaScript Function Passing an Object ArgumentSee more JavaScript ExamplesDemonstrates how to call a JavaScript function with an argument that is an object.Note: This example requires Chilkat v11.4.0 or greater.
integer li_rc integer li_Success oleobject loo_SbScript oleobject loo_Js oleobject loo_Result oleobject loo_FuncCall oleobject loo_Arg oleobject loo_ArgsArray oleobject loo_SbOut li_Success = 0 // This is the JavaScript function we'll call: // function describeCar(car) { // console.log(`This is a ${car.year} ${car.make} ${car.model}.`); // } loo_SbScript = create oleobject li_rc = loo_SbScript.ConnectToNewObject("Chilkat.StringBuilder") if li_rc < 0 then destroy loo_SbScript MessageBox("Error","Connecting to COM object failed") return end if loo_SbScript.Append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }") loo_Js = create oleobject li_rc = loo_Js.ConnectToNewObject("Chilkat.Js") loo_Result = create oleobject li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject") loo_Result.EmitCompact = 0 // Call Eval to add the function to the context's global object li_Success = loo_Js.Eval(loo_SbScript,loo_Result) if li_Success = 0 then // Examine the result for an exception. Write-Debug loo_Result.Emit() // Also examine the LastErrorText. Write-Debug loo_Js.LastErrorText destroy loo_SbScript destroy loo_Js destroy loo_Result return end if // ------------------------------------------------------------------------------ // Call the function describeCar(car) loo_FuncCall = create oleobject li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject") loo_FuncCall.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 // } // ] // } loo_FuncCall.UpdateString("name","describeCar") // Create the JSON object that is the argument. loo_Arg = create oleobject li_rc = loo_Arg.ConnectToNewObject("Chilkat.JsonObject") loo_Arg.UpdateString("make","Toyota") loo_Arg.UpdateString("model","Corolla") loo_Arg.UpdateInt("year",2022) // Create the arguments array. loo_ArgsArray = create oleobject li_rc = loo_ArgsArray.ConnectToNewObject("Chilkat.JsonArray") loo_ArgsArray.AddObjectCopyAt(0,loo_Arg) // Add the "args" array to the funcCall. loo_FuncCall.AppendArrayCopy("args",loo_ArgsArray) Write-Debug loo_FuncCall.Emit() li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result) if li_Success = 0 then // Examine the result for an exception. Write-Debug loo_Result.Emit() // Also examine the LastErrorText. Write-Debug loo_Js.LastErrorText destroy loo_SbScript destroy loo_Js destroy loo_Result destroy loo_FuncCall destroy loo_Arg destroy loo_ArgsArray return end if Write-Debug loo_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. loo_SbOut = create oleobject li_rc = loo_SbOut.ConnectToNewObject("Chilkat.StringBuilder") loo_Js.ConsoleOutputSb(loo_SbOut) Write-Debug loo_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: loo_FuncCall.Clear() loo_FuncCall.UpdateString("name","describeCar") loo_FuncCall.UpdateString("args[0].make","Toyota") loo_FuncCall.UpdateString("args[0].model","Corolla") loo_FuncCall.UpdateInt("args[0].year",2022) Write-Debug loo_FuncCall.Emit() destroy loo_SbScript destroy loo_Js destroy loo_Result destroy loo_FuncCall destroy loo_Arg destroy loo_ArgsArray destroy loo_SbOut |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.