![]() |
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
(Visual FoxPro) 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.
LOCAL lnSuccess LOCAL loSbScript LOCAL loJs LOCAL loResult LOCAL loFuncCall LOCAL loArg LOCAL loArgsArray LOCAL loSbOut lnSuccess = 0 * This is the JavaScript function we'll call: * function describeCar(car) { * console.log(`This is a ${car.year} ${car.make} ${car.model}.`); * } loSbScript = CreateObject('Chilkat.StringBuilder') loSbScript.Append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }") loJs = CreateObject('Chilkat.Js') loResult = CreateObject('Chilkat.JsonObject') loResult.EmitCompact = 0 * Call Eval to add the function to the context's global object lnSuccess = loJs.Eval(loSbScript,loResult) IF (lnSuccess = 0) THEN * Examine the result for an exception. ? loResult.Emit() * Also examine the LastErrorText. ? loJs.LastErrorText RELEASE loSbScript RELEASE loJs RELEASE loResult CANCEL ENDIF * ------------------------------------------------------------------------------ * Call the function describeCar(car) loFuncCall = CreateObject('Chilkat.JsonObject') loFuncCall.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 * } * ] * } loFuncCall.UpdateString("name","describeCar") * Create the JSON object that is the argument. loArg = CreateObject('Chilkat.JsonObject') loArg.UpdateString("make","Toyota") loArg.UpdateString("model","Corolla") loArg.UpdateInt("year",2022) * Create the arguments array. loArgsArray = CreateObject('Chilkat.JsonArray') loArgsArray.AddObjectCopyAt(0,loArg) * Add the "args" array to the funcCall. loFuncCall.AppendArrayCopy("args",loArgsArray) ? loFuncCall.Emit() lnSuccess = loJs.CallFunction(loFuncCall,loResult) IF (lnSuccess = 0) 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 CANCEL 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('Chilkat.StringBuilder') 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.