Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
How can I access an instance object from other script?

I have two script: 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));

And scipt2.js from incline html:

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

In $( document ).ready(function().. from script2.js, I want to call the function update from scipt1.js after doA executed. Is there anyway I can reference the instance newA and call that function update?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use .data to associate the element(s) doA is called on with an instance, and then .data can be used elsewhere to retrieve it.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda