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

224
Views
¿Cómo puedo acceder a un objeto de instancia desde otro script?

Tengo dos secuencias de comandos: scipt1.js:

 (function ($) { 'use strict'; var A = function (element, options) { this.element = $(element); }; A.prototype.update = function update() {} $.fn.doA = function (option) { return this.each(function test() { newA = new A(this, options); } }); }; }(jQuery));

Y scipt2.js de html inclinado:

 $( document ).ready(function(){ $('#$this->id').doA($javascriptParameterString); });

En $(documento).ready(function().. from script2.js, quiero llamar a la actualización de la función desde scipt1.js después de ejecutar doA. ¿Hay alguna forma en que pueda hacer referencia a la instancia newA y llamar a esa función update?

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

0

Use .data para asociar los elementos a los que se llama doA con una instancia, y luego .data se puede usar en otro lugar para recuperarlo.

 var A = function(element, options) { this.element = $(element); this.options = options; }; $.fn.doA = function(options) { return this.each(function test() { $(this).data('a', new A(this, options)); }); }; // Other script: $(document).ready(function() { $('.foo').doA({ property: 'foo' }); console.log('now iterating over instantiated As:'); for (const foo of $('.foo')) { const a = $(foo).data('a'); console.log(a.options); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="foo"></div>

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!