Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Se necesita una pieza de código JS para usar varias veces con diferentes valores y nombres de funciones. ¿Cómo hacerlo de manera eficiente?

Tengo un script que es así:

 <div id="foo"></div> <script> var foo; bar() function bar() { var config = { a: "foo", b: "string", otherConfig1: ... otherConfig2: ... ... } foo = new NeoVis.default(config) foo.render(); console.log(foo); } </script>

Quiero tener 8 piezas más de esto, con diferentes foo , bar y string . Los otherConfig s no se modifican. Simplemente puedo duplicarlos, pero dado que los otherConfig son muy largos, me gustaría hacerlo de manera más inteligente. He leído sobre constructor , pero como la función no tiene ningún argumento, no sé cómo aplicar esto. También trato de mover el objeto de config fuera de la función, pero parece que el código no se ejecuta.

Neovis es una biblioteca. También pregunto esto en Github de Neovis: ¿Cómo tener múltiples divs con diferentes consultas iniciales de Cypher?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Podría implementar una función a la que le pase id , func , string como parámetros para generar una function en el context que prefiera (probablemente window ). He implementado un NeoVis ficticio por el bien de la prueba, deberá usar el suyo en su lugar.

 var NeoVis = { default: function(config) { this.render = function() {}; } }; function myfunction(context, id, func, str) { context[id] = undefined; return context[func] = function() { var config = { a: id, b: str, //otherConfigs... }; context[id] = new NeoVis.default(config); context[id].render(); console.log(`function ${func} was called and ${id} is the id`); }; } for (let item of document.querySelectorAll("#foo, #lorem, #dolor")) { myfunction(window, item.id, item.getAttribute("data-function"), item.innerText)(); }
 <div id="foo" data-function="bar">string1</div> <div id="lorem" data-function="ipsum">string2</div> <div id="dolor" data-function="es">string3</div>

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!