![]() |
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
(Lianja) 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.
llSuccess = .F. // This is the JavaScript function we'll call: // function describeCar(car) { // console.log(`This is a ${car.year} ${car.make} ${car.model}.`); // } loSbScript = createobject("CkStringBuilder") loSbScript.Append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }") loJs = createobject("CkJs") loResult = createobject("CkJsonObject") loResult.EmitCompact = .F. // Call Eval to add the function to the context's global object llSuccess = loJs.Eval(loSbScript,loResult) if (llSuccess = .F.) then // Examine the result for an exception. ? loResult.Emit() // Also examine the LastErrorText. ? loJs.LastErrorText release loSbScript release loJs release loResult return endif // ------------------------------------------------------------------------------ // Call the function describeCar(car) loFuncCall = createobject("CkJsonObject") loFuncCall.EmitCompact = .F. // 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 // } // ] // } loFuncCall.UpdateString("name","describeCar") // Create the JSON object that is the argument. loArg = createobject("CkJsonObject") loArg.UpdateString("make","Toyota") loArg.UpdateString("model","Corolla") loArg.UpdateInt("year",2022) // Create the arguments array. loArgsArray = createobject("CkJsonArray") loArgsArray.AddObjectCopyAt(0,loArg) // Add the "args" array to the funcCall. loFuncCall.AppendArrayCopy("args",loArgsArray) ? loFuncCall.Emit() llSuccess = loJs.CallFunction(loFuncCall,loResult) if (llSuccess = .F.) then // Examine the result for an exception. ? loResult.Emit() // Also examine the LastErrorText. ? loJs.LastErrorText release loSbScript release loJs release loResult release loFuncCall release loArg release loArgsArray return endif ? loResult.Emit() // The describeCar JavaScript function returns nothing. // Therefore, the result is "undefined". // { // "type": "undefined", // "value": "undefined" // } // However, the function emitted text to the console. loSbOut = createobject("CkStringBuilder") loJs.ConsoleOutputSb(loSbOut) ? loSbOut.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: loFuncCall.Clear() loFuncCall.UpdateString("name","describeCar") loFuncCall.UpdateString("args[0].make","Toyota") loFuncCall.UpdateString("args[0].model","Corolla") loFuncCall.UpdateInt("args[0].year",2022) ? loFuncCall.Emit() release loSbScript release loJs release loResult release loFuncCall release loArg release loArgsArray release loSbOut |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.