博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件从头开始读函数_这是您可以从头开始编写的一些函数修饰符
阅读量:2524 次
发布时间:2019-05-11

本文共 3513 字,大约阅读时间需要 11 分钟。

文件从头开始读函数

was named one of the !

被评为

A function decorator is a higher-order function that takes one function as an argument and returns another function, and the returned function is a variation of the argument function —

函数装饰器是一个高阶函数,它将一个函数作为参数并返回另一个函数,返回的函数是参数函数的变体—

Let’s write some common function decorators found in libraries like , or .

让我们写一些在 , 或类的库中找到的常用函数修饰符。

一旦() (once())

  • : creates a version of the function that executes only once. It’s useful for an initialization function, where we want to make sure it runs only once, no matter how many times it is called from different places.

    :创建仅执行一次的函数版本。 这对于初始化函数很有用,因为我们希望确保它只运行一次,而不管它在不同位置被调用了多少次。

function once(fn){  let returnValue;  let canRun = true;  return function runOnce(){      if(canRun) {          returnValue = fn.apply(this, arguments);          canRun = false;      }      return returnValue;  }}var processonce = once(process);processonce(); //processprocessonce(); //

once() is a function that returns another function. The returned function runOnce() is a . It’s also important to note how the original function was called — by passing in the current value of this and all the arguments : fn.apply(this, arguments) .

once()是一个返回另一个函数的函数。 返回的函数runOnce()是一个 。 同样重要的是要注意原始函数是如何调用的—通过传入this和所有arguments的当前值: fn.apply(this, arguments)

If you want to understand closures better, take a look at .

如果您想更好地理解闭包,请查看 。

后() (after())

  • : creates a version of the function that executes only after a number of calls. It’s useful, for example, when we want to make sure the function runs only after all the asynchronous tasks have finished.

    :创建仅在多次调用后才执行的函数版本。 例如,当我们要确保该功能仅在所有异步任务完成后才运行时,此功能很有用。

function after(count, fn){   let runCount = 0;   return function runAfter(){      runCount = runCount + 1;      if (runCount >= count) {         return fn.apply(this, arguments);              }   }}function logResult() { console.log("calls have finished"); }let logResultAfter2Calls = after(2, logResult);setTimeout(function logFirstCall() {       console.log("1st call has finished");       logResultAfter2Calls(); }, 3000);setTimeout(function logSecondCall() {       console.log("2nd call has finished");       logResultAfter2Calls(); }, 4000);

Note how I’m using after() to build a new function logResultAfter2Calls() that will execute the original code of logResult() only after the second call.

注意我如何使用after()建立一个新的功能logResultAfter2Calls()将执行的原代码logResult()第二个电话后只。

风门() (throttle())

  • : creates a version of the function that, when invoked repeatedly, will call the original function once per every wait milliseconds. It’s useful for limiting events that occur faster.

    :创建函数的版本,当反复调用该函数时,将每wait毫秒调用一次原始函数。 这对于限制更快发生的事件很有用。

function throttle(fn, interval) {    let lastTime;    return function throttled() {        let timeSinceLastExecution = Date.now() - lastTime;        if(!lastTime || (timeSinceLastExecution >= interval)) {            fn.apply(this, arguments);            lastTime = Date.now();        }    };}let throttledProcess = throttle(process, 1000);$(window).mousemove(throttledProcess);

In this example, moving the mouse will generate a lot of mousemove events, but the call of the original function process() will just happen once per second.

在此示例中,移动鼠标将生成许多mousemove事件,但是原始函数process()的调用仅每秒发生一次。

was named one of the !

被称为

For more on applying functional programming techniques in React take a look at .

有关在React中应用函数式编程技术的更多信息,请查看

Learn functional React, in a project-based way, with .

通过 ,以基于项目的方式学习功能性React

翻译自:

文件从头开始读函数

转载地址:http://fjwzd.baihongyu.com/

你可能感兴趣的文章
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>
【Luogu】P2498拯救小云公主(spfa)
查看>>
如何获取网站icon
查看>>
几种排序写法
查看>>
java 多线程的应用场景
查看>>
dell support
查看>>
转:Maven项目编译后classes文件中没有dao的xml文件以及没有resources中的配置文件的问题解决...
查看>>