[SOLVED] Javascript: How to access the current index value inside an array stored function


If a function is stored in an array item, how can I gain access of the array’s current index value inside the function?
For example, how can I set my function in order to get results similar to this?

arr['hello'](); // function returns 'hello'
arr['world'](); // function returns 'world'
arr[123]() // function returns 123


Unless you know in advance which specific properties will be used, you’ll need a Proxy.

const prox = new Proxy(
    get(_, prop) {
      return () => prop;


Since you have some non-numeric properties, you shouldn’t use an array.

Answered By – CertainPerformance

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *