试图重新创建_。Once函数,但仅在至少2个迭代中起作用

发布于 2025-02-12 11:32:08 字数 811 浏览 1 评论 0原文

onceCopy function (testFunc) {
  const copyFunc = (a) => {
    const copyFunc2 = (b) => {
      return testFunc(a);
    };    
    return copyFunc2;
  };
  return copyFunc;
};

因此,该函数在第一次调用时返回内部函数。

然后返回第二个调用的内部函数的内部函数。

然后,第二个内部函数(第三个调用)实际上返回父函数中传递的参数,仅使用我们在第二个调用中给它的字符调用。

理想情况下,我想实现仅在第一个召唤之后,如果这是有道理的,我就可以实现许多调用。

编辑:是的,对不起,_.once。

编辑:因此,首先调用Oncopy应该保留通过 第二个调用应触发副本,并给出一个OUPUT 第三个调用应给出第二次调用的结果,因此第四,第五,第六等...

我的功能确实会这样做,但是在第二个调用中,它再次存储一个函数(copyfunc2),但是我只是这样做了,因为我需要在某个地方存储“ A”。

因此,就像我们有

函数pultiplyby3(a){返回A*3} 然后,一旦复制存储了乘数Bultyby3

const actualfunction = oncecopy(pultlyby3)

然后在第二和第三个调用时,我想要的是

实际功能(1)= 3 实际功能(66)= 3

,因此传递的函数只能运行一次,

无法解释更多,它在lodash文档中。

onceCopy function (testFunc) {
  const copyFunc = (a) => {
    const copyFunc2 = (b) => {
      return testFunc(a);
    };    
    return copyFunc2;
  };
  return copyFunc;
};

So the function returns the inner function upon first invocation.

Then returns the inner function of the inner function of the second invocation.

Then the second inner function (third invocation) actually returns the passed argument in the parent function and only invokes it with the character we gave it on the second invocation.

Ideally I want to achieve what I'm achieving over many invocations after only the first one if that makes sense.

Edit: Yes sorry, _.once.

Edit: so first invocation onceCopy SHOULD hold a copy of the Func passed
Second invocation SHOULD trigger the copy and gives an ouput
Third invocation SHOULD give the result of the second invocation so should the fourth, fifth, sixth and so on...

My function does do this, but on the second invocation it stores a function (copyFunc2) again, but I just made that because I need somewhere to store "a".

so like we have

function multiplyBy3 (a) {return a*3}
and then once copy stores a copy of multiplyBy3

const actualFunction = onceCopy(multiplyBy3)

then upon second and third invocation what I want

actualFunction(1) = 3
actualFunction(66) = 3

so the passed function ONLY RUNS ONCE

Cant explain more than this, its in the lodash docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习ぎ惯性依靠 2025-02-19 11:32:08

我不熟悉您要重新实现的功能,因此,如果我误解了我,请随时纠正我。要包装一个功能并确保只有一旦您不需要多个嵌套包装就可以调用,只有一个具有某种状态。

您需要跟踪是否已经有结果返回(Hasreult),如果是这样,则结果是(result)。将这两个变量保持分开,可以使您在结果 IS undefined时介绍情况,同时保持代码易于阅读和理解。

function once(wrappedFunction) {
  let hasResult = false;
  let result;
  return (...args) => {
    if (hasResult) {
      return result;
    }
    result = wrappedFunction.apply(this, args);
    hasResult = true;
    return result;
  }
}

// An example function to wrap
function multiply(a, b) {
  return a * b;
}

const demoFunction = once(multiply)
console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2

I'm not familiar with the function you're trying to reimplement, so feel free to correct me if I misunderstood. To wrap a function and ensure it's only called once you don't need multiple nested wrappings, only one with some state.

You need to keep track of whether you already have a result to return (hasResult) and if so, what that result is (result). Keeping these two variables separate allows you to cover the case when result is undefined while keeping the code easy to read and understand.

function once(wrappedFunction) {
  let hasResult = false;
  let result;
  return (...args) => {
    if (hasResult) {
      return result;
    }
    result = wrappedFunction.apply(this, args);
    hasResult = true;
    return result;
  }
}

// An example function to wrap
function multiply(a, b) {
  return a * b;
}

const demoFunction = once(multiply)
console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2

浮云落日 2025-02-19 11:32:08

这是您想要的吗?

初始功能返回保留在后续呼叫上。我使用了第二个变量,称为,如果第一个呼叫返回未定义,也应在后续呼叫上返回。

const once = (onceFn) => {
  let called;
  let value;

  return (...args) => {
    if (called) return value;
    called = true;
    return (value = onceFn(...args));
  };
};

function multiplyBy3(a) {
  return a * 3;
}

const fn = once(multiplyBy3);

console.log(fn(3)); // 9
console.log(fn(66)); // 9

Is this what you were looking for?

The initial function return is kept on subsequent calls. I used a second variable called in case the first call returns undefined, which should also be returned on subsequent calls.

const once = (onceFn) => {
  let called;
  let value;

  return (...args) => {
    if (called) return value;
    called = true;
    return (value = onceFn(...args));
  };
};

function multiplyBy3(a) {
  return a * 3;
}

const fn = once(multiplyBy3);

console.log(fn(3)); // 9
console.log(fn(66)); // 9
时光沙漏 2025-02-19 11:32:08

在第一次调用该功能并获得结果后,创建一个返回结果的新功能,并在称为包装函数时使用它:

const once = fn => {
  let func
  
  return (...args) => {
    if(func) return func()
    
    const result = fn(...args)
    
    func = () => result
    
    return result
  } 
}

const multiply = (a, b) => a * b

const demoFunction = once(multiply)

console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2

After calling the function for the 1st time, and getting the result, create a new function that returns the result, and use it whenever the wrapped function is called:

const once = fn => {
  let func
  
  return (...args) => {
    if(func) return func()
    
    const result = fn(...args)
    
    func = () => result
    
    return result
  } 
}

const multiply = (a, b) => a * b

const demoFunction = once(multiply)

console.log(demoFunction(1, 2)); // 2
console.log(demoFunction(3, 4)); // 2
console.log(demoFunction(5, 6)); // 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文