October 22, 2024
Chicago 12, Melborne City, USA
javascript

How can I count how many times a function argument has been used in JS?


I need to construct a function, which takes a black-box one-argument function as an argument. And I need to know, how many times the black-box function uses its argument

Example signature:

function argCounter(fn) {
  return (arg) => {
    let argCalls = 0

    ...

    fn(arg)

    return argCalls
  }
}

Example use:

const fn1 = argCounter(obj => {
  return obj
}

fn1() // 1

const fn2 = argCounter(obj => {
  obj; obj; obj;
  return obj
}

fn2() // 4

I know about JS Proxy, but it only gives me the ability to count how many times an object’s methods or attributes have been called. Unfortunately, I couldn’t determine, how to use JS Proxy to count, how many times the object itself has been used.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video