1

I have been working to create a function that given another function will make that second function only callable once. not unlike the _.once() function.

the desired outcome is the following:

const oneTimeFunction = _.once(function(string) { string.split(''); })

oneTimeFunction('hello')

//returns: 'olleh', and if called again it would have no effect returning the same thing a the original call.

Currently this is what I have:

_.once = function (func) {
  var called = 0;
  let args = null;

  if (arguments.length > 1) {
    args = Array.prototype.slice.call(arguments,1);
  }

  return function () {
    if (called === 0) {
      console.log('being called');
      called ++;

      if (!args) {
        console.log('without apply');
        return func.call(arguments);
      } else {
        console.log('with apply');
        return func.apply(this,args);
      }
    } else {
      console.log('this has been called');
      return null;
    }
  }; 
};

I am running into a wall as it is returning error type undefined even with everything I have tried. Any help, even to get to where it can call the function regardless of the one time only stipulation? Thanks!

4
  • One thing is that you are doing arguments processing at the wrong place. You are processing the arguments to .once() instead of the arguments of function(string)... Commented Jun 25, 2021 at 7:50
  • Ah! that makes sense because the arguments of function(string) are a level deeper. I have created a simpler version of this that just gets the function(string) to work, but cannot get it to only be called once. Commented Jun 25, 2021 at 15:42
  • I now have started with this version that is doing the basic job of running the passed in func. I will try to add the one time functionality next. _.once = function (func) { console.log(typeof func); let oneTimer = func; return (function inner () { return oneTimer; })(); }; Commented Jun 25, 2021 at 15:47
  • Have you looked at the lodash source to study how it works? github.com/lodash/lodash/blob/master/once.js which simply calls before(2, function) github.com/lodash/lodash/blob/master/before.js Of course make changes as needed, but that will give you a good base to start from. Commented Jun 26, 2021 at 2:56

2 Answers 2

1

create a variable that count how much this function is called

let count = 0;
function once(str) {

  if(count < 1){
      count++;
      return str.split("").reverse().join("");
  }
  else return str;
}

console.log(once("hello")); // olleh
console.log(once("hello")); // hello
console.log(once("hello")); // hello
Sign up to request clarification or add additional context in comments.

3 Comments

This does not meet the specification that OP has made because subsequent calls return the argument's value, not the original call's value. "and if called again it would have no effect returning the same thing a the original call". Also, this is supposed to be a wrapper function, not a function with hardcoded string manipulation
Yeah thanks, but it works for me, the same as the output that I wrote, I know there's a code more readable and more useful than my!
If you call it once with "hello", every subsequent call has to return "olleh", no matter what input. It's not supposed to echo the input after the first call
1

In reading your question, I'm seeing that you would like to always return the first value on subsequent calls:

"if called again it would have no effect returning the same thing a[s] the original call."

So I believe you want to do something like this:

function computeOnce(myFn) {
  let origVal = undefined;

  return function (...args) {
    // if this is not set, this is the first call
    if (!origVal) {
      // execute the function and store it's return value
      origVal = myFn(...args);
    }
    return origVal;
  }
}

1 Comment

Good catch -- I missed the bit about returning the same value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.