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

161
Views
how do you assign a dictionary of functions to javascript addeventlistener?

I'm using this book to learn javascript: Secrets of the JavaScript Ninja, Second Edition.

In the book, there is code to store functions in a dictionary as a way to manage multiple functions in an event handler.

var store = {
        nextId: 1,
        cache: {},
        add: function(fn){
            if (!fn.id) {
                fn.id = this.nextId++;
                this.cache[fn.id] = fn;
                return true;
            }
        }
    }
store.add(myFunction1);
store.add(myFunction2);
store.add(myFunction3);

But the book doesn't explain how to assign these functions to an event handler once they are stored the the dictionary store.cache.

So my question is, how does one assign these functions, straight from the dictionary, into the addEventListener function? I tried pulling the functions out individually, such as store.cache[1].name, but then an error pops up saying the parameter is not of type 'Object.' I also tried to use a loop to get the function names, but the names come back as strings and the event listener doesn't call the functions when the names are strings. What am I missing here? Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can refer to them by their numerical order in the array. Then still pass any parameters as needed.

let myFunction1 = function(p) {
  console.log(p)
}
var store = {
  nextId: 1,
  cache: {},
  add: function(fn) {
    if (!fn.id) {
      fn.id = this.nextId++;
      this.cache[fn.id] = fn;
      return true;
    }
  }
}
store.add(myFunction1);


store.cache[1]("x");

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!