带有返回值的装饰器

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

“发现功能JavaScript”BookAuthority评为最佳新功能编程书籍之一

Method decorators are a tool for reusing common logic. They are complementary to Object Oriented Programming. Decorators encapsulate responsibility shared by different objects.

方法装饰器是用于重用通用逻辑的工具。 它们是面向对象编程的补充。 装饰器封装了不同对象共享的责任。

Consider the following code:

考虑以下代码 :

function TodoStore(currentUser){let todos = [];function add(todo){let start = Date.now();if(currentUser.isAuthenticated()){todos.push(todo);} else {throw "Not authorized to perform this operation";}let duration = Date.now() - start;console.log("add() duration : " + duration);}return Object.freeze({add});
}

The intent of the add() method is to add new to-dos to the internal state. Beside that, the method needs to check the user authorization and log the duration of execution. These two things are secondary concerns and can actually repeat in other methods.

add()方法的目的是向内部状态添加新的待办事项。 除此之外,该方法还需要检查用户授权并记录执行时间。 这两件事是次要问题,实际上可以在其他方法中重复。

Imagine we can encapsulate these secondary responsibilities in functions. Then we can write the code in the following way:

假设我们可以将这些次要责任封装在功能中。 然后我们可以通过以下方式编写代码:

function TodoStore(){let todos = [];function add(todo){todos.push(todo);}return Object.freeze({add:compose(logDuration,authorize)(add) });
}

Now the add() method just adds the todo to the list. The other responsibilities are implemented by decorating the method.

现在, add()方法仅将todo添加到列表中。 其他职责通过装饰方法来实现。

logDuration() and authorize() are decorators.

logDuration()authorize()是装饰器。

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.

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

Reginald Braithwaite in Javascript Allongé

Reginald Braithwaite使用JavascriptAllongé

日志持续时间 (Log Duration)

A common scenario is logging the duration of a method call. The following decorator logs the duration of a synchronous call.

一种常见的情况是记录方法调用的持续时间。 以下装饰器记录同步呼叫的持续时间。

function logDuration(fn){return function decorator(...args){let start = Date.now();let result = fn.apply(this, args);let duration = Date.now() - start;console.log(fn.name + "() duration : " + duration);return result;}
}

Notice how the original function was called — by passing in the current value of this and all arguments : fn.apply(this, args) .

注意原始函数是如何被调用的—通过传入this和所有参数的当前值: fn.apply(this, args)

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

发现功能JavaScript被称为 BookAuthority最好的新功能编程书籍

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

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

Learn functional React, in a project-based way, with Functional Architecture with React and Redux.

通过带有React和Redux的功能架构 ,以基于项目的方式学习功能性React

Follow on Twitter

在Twitter上关注

翻译自: https://www.freecodecamp.org/news/how-to-use-decorators-with-factory-functions-373fb972b6d4/

带有返回值的装饰器

带有返回值的装饰器_如何使用带有工厂功能的装饰器相关推荐

  1. java开启线程的方法_Java有几种方法开启线程?怎么实现带有返回值的线程?

    Java有几种方法开启线程?怎么实现带有返回值的线程? 发布时间:2020-05-22 11:20:08 来源:亿速云 阅读:238 作者:Leah Java有几种方法开启线程?怎么实现带有返回值的线 ...

  2. fread读结构体返回值是0无错误_嵌入式C编程之错误处理(附代码例子)!

    原标题:嵌入式C编程之错误处理(附代码例子)! 作者: clover-toeic 前言 本文主要总结嵌入式系统C语言编程中,主要的错误处理方式.文中涉及的代码运行环境如下: 一.错误概念 错误分类 从 ...

  3. java 返回值判断_在Java中判断方法重载的条件除了参数外,还可以通过返回值类型判断。_学小易找答案...

    [单选题]若int x;且有下面的程序片断,则输出结果是() . for (x=3; x<6; x++) { printf((x%2) ? "##%d" : "** ...

  4. 返回值类型有哪些_关于类型的思考

    ts用得很爽,实现了我心目中关于类型的想法.类型!=面向对象.类型甚至不是真实存在的,运行期会移除,只是辅助代码编写.类型是别名,本质上只要满足鸭辨就是同一类型.联合类型的区分太智能了,再以联合字符串 ...

  5. php中strrpos函数的返回值类型是型_函数strrpos('Welcome to learning PHP','e')的返回值是___________。...

    [简答题]参照资料中"实训3-5用户登录和退出"的要求,完成实训内容,将实训报告作为答案附件提交.实训报告文件名格式:"实训3-5用户登录和退出20170215xx姓名& ...

  6. php中strrpos函数的返回值类型是型_函数strrpos('Welcome to learning PHP', 'e')的返回值是______...

    函数strrpos('Welcome to learning PHP', 'e')的返回值是______ 答:12 我们可以利用字符发生存储器CGRAM编制并显示标准字库表中没有的字符 答:√ 不属于 ...

  7. python进入上下文管理器_浅谈Python中with(上下文管理器)的用法

    例子一 首先来看一段代码: classFoo(object):def __init__(self):print('实例化一个对象')def __enter__(self):print('进入')def ...

  8. excel切片器_不喜欢Excel自带的切片器样式,我一秒设置个性化切片器

    一般在excel中插入的切片器都是默认样式的,即使根据自带模板修改之后也不是很满意的话,小编来教大家设置自己的切片器吧! 库存切片器 第一步:插入切片器 第二步:选择产品名称确定,更改产品名称后实时显 ...

  9. excel切片器_听说你还不会用切片器?比筛选好用100倍,小白也能学会

    私信回复关键词[2020]~ 获取100+套高颜值图表模板! 大家好,我是还在写毕业论文的小E~ 临近毕业,还要搞 PPT!心累啊! 这不,我在看关于怎么做毕业论文 PPT 的时候,无意中看到了这样的 ...

最新文章

  1. iactionresult 图片_从显式类型的ASP.NET Core API控制器(不是IActionResult)返回404
  2. 借助Fargate和EKS,AWS甚至可以实现Cloud-ier和Kuberneties-ier
  3. Python Django之GET请求和POST请求及响应处理
  4. 【2016年第2期】大数据背景下的治理现代化:何以可能与何以可为(上)
  5. 【Spark】一条 SQL 在 Apache Spark 之旅(上)
  6. Android、Java泛型扫盲
  7. 瞎折腾-CentOS 7.4 编译4.16.2版kernel 并安装
  8. 查看Linux服务器架构是x86还是arm
  9. vertical-align属性
  10. 如何判断电脑是否中毒(转)
  11. 南航大学计算机专业硕士学历,南航计算机科学与技术学院导师介绍:戴群
  12. 高等代数期末考试题库及答案_数学类高等代数期末考试试题A卷(含答案)
  13. 【算法与数据结构】—— 并查集
  14. 有赞实时数仓建设实践与经验
  15. sigmoid函数及其导数
  16. google即将进入游戏产业
  17. 东北大学计算机考研专业842包括什么,2020年东北大学842《计算机专业基础》考研大纲...
  18. java(Springboot) excel模板下载、导入
  19. 图像特征提取与匹配——SIFT算法
  20. java用wasd_涨知识:游戏默认WASD原来是这么来的

热门文章

  1. Linux完全删除用户
  2. 2018年黑龙江由俄进口原油2725.2万吨同比增加67.1%
  3. WinForm下屏幕截图程序的实现
  4. input框带图片的写法
  5. iscroll上拉加载、下拉刷新
  6. SQL Server 日志传送
  7. ElasticSearch面试 - es 生产集群的部署架构是什么?
  8. Docker快速搭建JIRA缺陷管理平台
  9. Fedora的systemctl命令详解 Fedora配置IP地址/DNS/Networking
  10. python读取html文件中的表格数据_使用解析html表pd.read_html文件其中单元格本身包含完整表...