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

144
Vistas
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 Respuestas
Responde la pregunta

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 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