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

React Native
import { Js, JsonArray, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This is the JavaScript function we'll call:

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

  const sbScript = new StringBuilder();
  sbScript.append('function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }');

  const js = new Js();

  const result = new JsonObject();
  result.emitCompact = false;

  // Call Eval to add the function to the context's global object
  try {
    await js.evalAsync(sbScript, result);
  } catch {
    // Examine the result for an exception.
    console.log(result.emit());

    // Also examine the LastErrorText.
    console.log(js.lastErrorText);
    return;
  }

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

  const funcCall = new JsonObject();
  funcCall.emitCompact = 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
  //     }
  //   ]
  // }

  funcCall.updateString('name', 'describeCar');

  // Create the JSON object that is the argument.
  const arg = new JsonObject();
  arg.updateString('make', 'Toyota');
  arg.updateString('model', 'Corolla');
  arg.updateInt('year', 2022);

  // Create the arguments array.
  const argsArray = new JsonArray();
  argsArray.addObjectCopyAt(0, arg);

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

  console.log(funcCall.emit());

  try {
    await js.callFunctionAsync(funcCall, result);
  } catch {
    // Examine the result for an exception.
    console.log(result.emit());

    // Also examine the LastErrorText.
    console.log(js.lastErrorText);
    return;
  }

  console.log(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.

  const sbOut = new StringBuilder();
  js.consoleOutputSb(sbOut);
  console.log(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:

  funcCall.clear();
  funcCall.updateString('name', 'describeCar');
  funcCall.updateString('args[0].make', 'Toyota');
  funcCall.updateString('args[0].model', 'Corolla');
  funcCall.updateInt('args[0].year', 2022);
  console.log(funcCall.emit());
}