Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
How to construct a function from dictionary that contains function name and arguments typescript

I am having a dictionary

{
   "function_name":"myFunc",
   "arguments":["a1","a2"]
}

I want to construct a function where the function name is the function name that is present in the above dictionary (i.e myFunc ) with respective arguments (i.e ["a1","a2"]).

The final output of the generator should be:

myFunc(a1,a2){

}

Actual Usage: I want to add this function to a class instance and invoke it

and extending this.

If this function is async then I should be able to await it.

Example:

for a smart contract to call a function we generally do (Referring to greeter smart contract )

contract.functions.greet().then(k => console.log(k))

The contract function type is:

 readonly functions: { [ name: string ]: ContractFunction };
 export type ContractFunction<T = any> = (...args: Array<any>) => Promise<T>;

using ethers library.

I wanted to generate the greet function using the contract ABI dynamically :

[
    {
      "inputs": [
        {
          "internalType": "string",
          "name": "_greeting",
          "type": "string"
        },
      ],
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "inputs": [],
      "name": "greet",
      "outputs": [
        {
          "internalType": "string",
          "name": "",
          "type": "string"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [
        {
          "internalType": "string",
          "name": "_greeting",
          "type": "string"
        }
      ],
      "name": "setGreeting",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ]

I am able to parse the above JSON and get the function name and arguments. Now as a final step I want to attach this constructed function to the contract and call it.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I guess you're trying to do something like the following:

class SomeClass {
  obj = {
    functionName: "add",
    args: [10, 20],
  };

  add(a, b) {
    return a + b;
  }

  invokeFunc() {
    const { functionName, args } = this.obj;
    return this[functionName](...args);
  }
}

const instance = new SomeClass();
console.log(instance.invokeFunc());

Since the function to be invoked is accessible from the this object, you can invoke it and spread (...) all the arguments.

For async functions it's not clear how it's indicated that it's an async function. You can have a type field in the object and conditionally await the function.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example on how to do this.

read here https://www.c-sharpcorner.com/article/creating-functions-dynamically-in-javascript/ as this could help you

var strFunc = [{
  "isAsync":false,
  "function_name": "myFunc",
  "arguments": ["a1", "a2"],
  content: "return a1*a2"
},
{
  "isAsync":true,
  "function_name": "asyncMyFunc",
  "arguments": ["a1", "a2"],
  content: "return a1+a2"
}]

class Funcs {
constructor(f){
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
  f.forEach(x=> {
    let func = null;
    if (x.isAsync)
     func = new AsyncFunction(...x.arguments, x.content);
    else func = new Function(...x.arguments, x.content);
    
    this[x.function_name] = func;
  });

}

}

var mycl = new Funcs(strFunc);
mycl.asyncMyFunc(1,2).then(x=> console.log("asyncFunc", x))
console.log("syncFunc", mycl.myFunc(1,2));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda