Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!