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

146
Vistas
Send extra parameters with 'bind(this) while not losing 'event' object

I have an object literal pattern used in a code and I'm trying to pass the object name as reference to another function using 'bind'. I also need to pass a second parameter but when I do this I lose access to 'event' object.

var obj = {
  init: function() {
    this.trig();
  },

  trig: function() {
    $('.addButton').on('click', this.doSomething.bind(this, secondParameter))
  },

  doSomething: function(e,args) {
    e.preventDefault(); // erroring here e.preventDefault is not a function 

    //rest of the code 
  }
}

obj.init();

Any idea how I can send a second parameter whilst still having access to event object?

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

0

doSomething only accepts one parameter. If you want it to accept two then you need to write it that way.

See the MDN documentation for bind:

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

So you need to put variables to accept the bound arguments before the variable to accept the event object argument:

doSomething: function (boundArgument, e) {
about 4 years ago · Juan Pablo Isaza Denunciar

0

there is no need of using bind since doSomething is already a methode of the object so you just have to pass the event as argument

var obj = {
  init: function() {
    this.trig();
  }

  trig: function() {
    $('.addButton').on('click', (event) => this.doSomething(event))
  },

  doSomething: function(e) {
    e.preventDefault(); // erroring here e.preventDefault is not a function 

    //rest of the code 
  }
}

obj.init();
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