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

6.1K
Views
What is 'savedThis' for in the Throttle decorator solution on javascript.info?

On the https://javascript.info/call-apply-decorators there is the solution for the Throttling decorator task. I've modified the code to get rid of 'sevedThis' and it works perfectly without it (even with object methods). Could anybody give a real reason to use 'sevedThis' variable in this case and call 'wrapper.apply(savedThis, savedArgs)' instead of 'wrapper(...savedArgs)'?

Original code:

function throttle(func, ms) {

  let isThrottled = false,
    savedArgs,
    savedThis;

  function wrapper() {

    if (isThrottled) { // (2)
      savedArgs = arguments;
      savedThis = this;
      return;
    }
    isThrottled = true;

    func.apply(this, arguments); // (1)

    setTimeout(function() {
      isThrottled = false; // (3)
      if (savedArgs) {
        wrapper.apply(savedThis, savedArgs);
        savedArgs = savedThis = null;
      }
    }, ms);
  }

  return wrapper;
}

Code without 'savedThis':

 function throttle(func, ms) {
  let isThrottled = false,
    savedArgs;

  function wrapper() {
    if (isThrottled) {
      // (2)
      savedArgs = arguments;
      return;
    }
    func.apply(this, arguments); // (1)
    isThrottled = true;
    setTimeout(function () {
      isThrottled = false; // (3)
      if (savedArgs) {
        wrapper(...savedArgs);
        savedArgs = null;
      }
    }, ms);
  }
  return wrapper;
}
about 4 years ago · Juan Pablo Isaza
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!