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

141
Visualizações
JavaScript: Create a method in a key in an object if input of the main function exists

I am trying to create a variable Youtube Object that will be simulated in a for loop.

The concept is that I want to set a variable speed to each clip in a for loop. The idea is that YouTube takes in an object as described here. This seems difficult.

I created the function here:

function YTObjects(videoId1, ...args) {
  let object1 = {
    height: "315",
    width: "560",
    videoId: videoId1,
    playerVars: {
      autoplay: 0,
      loop: 1,
      ...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
      ...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
    },
    if ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined")) {
      event: function (args[2], args[3]) {
        player.setPlaybackRate(args[2]*args[3]);
      },
    }
  };
  return object1;
}

Which enables me to create a variable object. But I am failing to create a function in a key that depends on the arguments (2&3).

Can you please help in this regard?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Either use a ternary operator to conditionally assign the event property, or use an if after the object is defined. You probably don't want any params in the anonymous function that's being added.

  let object1 = {
    height: "315",
    width: "560",
    videoId: videoId1,
    playerVars: {
      autoplay: 0,
      loop: 1,
      ...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
      ...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
    },
    event: ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined"))
      ? function () {
        player.setPlaybackRate(args[2]*args[3]);
      },
      : undefined
  };
  return object1;

or

  let object1 = {
    height: "315",
    width: "560",
    videoId: videoId1,
    playerVars: {
      autoplay: 0,
      loop: 1,
      ...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
      ...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
    }
  };
  if ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined")) {
    object1.event = function () {
        player.setPlaybackRate(args[2]*args[3]);
    };
  }
  return object1;
about 4 years ago · Juan Pablo Isaza Relatório
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