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

93
Visualizações
JQuery passing parameters to custom function not working properly

Good day all,

I need some help figuring this jquery puzzle. spent quite sometime trying to get this working without luck. i need to be able to pass parameters to my custom function so that they are not persistent and can be overwritten. currently it will keep the firs set of parameters passed even though I passing new ones or nothing.

here is the code for the custom function that i attached to $

$(document).ready(() => {
  $.addImage = function addImage(params) {
    'use strinct';

    const ops = params || undefined;

    let cropper;

    let avatar = document.getElementById('Avatar');

    console.log(ops);
    // 1st call is $.addImage({ isKey1: 'yes' }) => { isKey1: 'yes' } (good)
    // 2nd call $.addImage() => { isKey1: 'yes' } nope should be undefined

    // 1st call is $.addImage() => undefined (good)
    // 2nd call $.addImage({ isKey1: 'yes' }) => undefined (nope should be the object)

    // we are in certification mode, change the image sources
    if (ops && ops.isKey1 === 'yes') {
      avatar = document.getElementById(rowImgID);
    }

    // eslint-disable-next-line prefer-arrow-callback
    document.getElementById('imgSave').addEventListener('click', function () {
      let canvas;

      if (cropper) {
        // eslint-disable-next-line prefer-arrow-callback
        canvas.toBlob(function (blob) {
          let url;

          console.log(ops);
          // 1st call is $.addImage({ isKey1: 'yes' }) => { isKey1: 'yes' } good
          // 2nd call $.addImage() => { isKey1: 'yes' } nope should be undefined or null

          // 1st call is $.addImage() => undefined (good)
          // 2nd call $.addImage({ isKey1: 'yes' }) => undefined (nope should be the object)

          if (ops && ops.isKey1 === 'yes') {
            url = 'url1';
          } else {
            url = 'url2';
          }

          console.log('url:', url);

          // $.ajax({
          //   // here send blob
          //   // send other values
          //   blob,
          //   .....
          //     .....
          // })
        });
      }
    });
  };
});

and when i try to call the function twice for example

first call without param like this

  $('#btnAddAvatar').on('click', (e) => {
    e.preventDefault();
    $.addImage();
  });

and second call somewhere else in the code like this

if ($target.hasClass('addImg')) {
  const options = {
    isKey1: 'yes',
    otherKey: otherValue,
  };
  $.addImage(options);
}

in the second call the "params" object always default to the value of the first call. so in this case "params" is null therefore ops becomes null. if I call $.addImage(options) first and then call $.addImage(), the second call will have all the options when I do not want the options.

about 4 years ago · Juan Pablo Isaza
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