First, what is 'High Order function', basic just a function, inside the function return another fuction.

// High order fucntion

function fn() {
return function(){ }
}

For example:

function compose(a, b) {
return function(c){
return a(b(c));
}
} function addTwo(val){
return val + 2;
} function tiemsTwo(val){
return val * 2;
} const val = compose(addTwo, tiemsTwo)(50);
console.info(val); // 102

Decorators is a subset of high order fucntion:

function fluent(fn){
return function(...args){
fn.apply(this, args);
return this;
}
} function Person(){} Person.prototype.setName = fluent(function(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}) Person.prototype.getName = fluent(function(){
console.log(this.firstName + ' ' + this.lastName);
}) var p = new Person(); console.log( p.setName('John', 'Kent').getName());

In this code, fluent actually decorate Person class, make it chainable.

But In ES6:

class Person {
setName(f, l) {
this.firstName = f;
this.lastName = l;
} getName() {
console.log(this.firstName, this.lastName);
}
}

We have no way to wrap setName and getName fucntion in fluent(). So that's way decorator comes in.

To use decorator to descorate a function in class:

function decorate(target, keys, descriptor){
var fn = descriptor.value; // Overwrite the value, which in this case is function
descriptor.value = function(...args){
fn.apply(target, args);
return target;
}
} class Person { @decorate
setName(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
} @decorate
getName(){
console.log(this.firstName, this.lastName);
}
} const p = new Person();
console.log(p.setName("Wan", "Zhentian").getName());

And it would be nice to reuse the fluent function:

function fluent(fn){
return function(...args){
fn.apply(this, args);
return this;
}
}

So we can do:

function fluent(fn){
return function(...args){
fn.apply(this, args);
return this;
}
} function decorateWith(fn){
return (target, keys, descriptor) => {
// fn here refers to setName or getName
// fn should be call with in target context, which means Person{}
// the second argument in call() is function to be passed into fluent() function
descriptor.value = fn.call(target, descriptor.value);
}
} class Person { @decorateWith(fluent)
setName(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
} @decorateWith(fluent)
getName(){
console.log(this.firstName, this.lastName);
}
} const p = new Person();
console.log(p.setName("Wan", "Zhentian").getName());

[Note]: call(context, arguement, arguement, ....); here arguement is sinlge value

apply(context, arguements): here arguements is array

[Javascript] Decorators in JavaScript的更多相关文章

  1. JavaScript权威设计--JavaScript函数(简要学习笔记十一)

    1.函数调用的四种方式 第三种:构造函数调用 如果构造函数调用在圆括号内包含一组实参列表,先计算这些实参表达式,然后传入函数内.这和函数调用和方法调用是一致的.但如果构造函数没有形参,JavaScri ...

  2. JavaScript权威设计--JavaScript函数(简要学习笔记十)

    1.函数命名规范 函数命名通常以动词为前缀的词组.通常第一个字符小写.当包含多个单词时,一种约定是将单词以下划线分割,就像"like_Zqz()". 还有一种就是"lik ...

  3. javascript笔记:javascript的关键所在---作用域链

    javascript里的作用域是理解javascript语言的关键所在,正确使用作用域原理才能写出高效的javascript代码,很多javascript技巧也是围绕作用域进行的,今天我要总结一下关于 ...

  4. JavaScript强化教程——JavaScript 总结

    本教程中我们向您讲授了如何向 html 页面添加 JavaScript,使得网站的动态性和交互性更强. 你已经学习了如何创建对事件的响应,验证表单,以及如何根据不同的情况运行不同的脚本. 你也学到了如 ...

  5. JavaScript学习13 JavaScript中的继承

    JavaScript学习13 JavaScript中的继承 继承第一种方式:对象冒充 <script type="text/javascript"> //继承第一种方式 ...

  6. Javascript学习2 - Javascript中的表达式和运算符

    原文:Javascript学习2 - Javascript中的表达式和运算符 Javascript中的运算符与C/C++中的运算符相似,但有几处不同的地方,相对于C/C++,也增加了几个不同的运算符, ...

  7. Javascript学习1 - Javascript中的类型对象

    原文:Javascript学习1 - Javascript中的类型对象 1.1关于Numbers对象. 常用的方法:number.toString() 不用具体介绍,把数字转换为字符串,相应的还有一个 ...

  8. 第一百二十九节,JavaScript,理解JavaScript库

    JavaScript,理解JavaScript库 学习要点: 1.项目介绍 2.理解JavaScript库 3.创建基础库 从本章,我们来用之前的基础知识来写一个项目,用以巩固之前所学.那么,每个项目 ...

  9. 使用Javascript/jQuery将javascript对象转换为json格式数据 - 海涛的CSDN博客 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

随机推荐

  1. hibernate的五大接口

    Hibernate有五大核心接口,分别是:Session Transaction Query SessionFactoryConfiguration .这五个接口构成了Hibernate运行的基本要素 ...

  2. Troubleshoot Refused VNC Connection in CentOS 7

    Troubleshoot Refused VNC Connection in CentOS 7 Posted on March 15, 2015 by Istvan Szarka — 2 Commen ...

  3. django CSRF token missing or incorrect

    django 异步请求时提示403 按照一般情况权限问题,python文件没有问题,仔细看了下response里有一句 CSRF token missing or incorrect.这个肯定是因为安 ...

  4. Chrome以https访问gitlab的问题:Your connection is not private

    在Chrome中以https访问自己搭建的gitlab站点时经常出现下面的错误: Attackers might be trying to steal your information from xx ...

  5. ASP.NET MVC 下拉列表使用小结

    ASP.NET MVC中下拉列表的用法很简单,也很方便,具体来说,主要是页面上支持两种Html帮助类的方法:DropDownList()和DropDownListFor().这篇博文主要作为个人的一个 ...

  6. Lambda应用设计模式

    前言 在使用 Lambda 表达式时,我们常会碰到一些典型的应用场景,而从常用场景中抽取出来的应用方式可以描述为应用模式.这些模式可能不全是新的模式,有的参考自 JavaScript 的设计模式,但至 ...

  7. 微软BI 之SSIS 系列 - 在 SSIS 中导入 ACCESS 数据库中的数据

    开篇介绍 来自 天善学院 一个学员的问题,如何在 SSIS 中导入 ACCESS 数据表中的数据. 在 SSIS 中导入 ACCESS 数据库数据 ACCESS 实际上是一个轻量级的桌面数据库,直接使 ...

  8. 免费好用的web应用托管平台-续

    上一篇博客给大家推荐了目前处于免费阶段的PAAS平台,可以托管各种应用,大家反响很不错,说明大家还是很需要和认可这个免费托管各种web应用的京东云擎平台.但是很多用户还是很担心未来可能还是会收费,对于 ...

  9. xml Schema import

    first_.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs=& ...

  10. 关于SQL SERVER的N前缀的理解

    加N前缀指定后面的字符串为UNICODE常量, SQL Server 的 标准中的国家字符数据类型.SQL 使用前缀字符 N 标识这些数据类型及其值. , ),使用 个 个 Unicode字符时,使用 ...