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. [原] XAF ListView显示隐藏Footer菜单

    using System; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Win.Editors; using DevExpress ...

  2. php byte数组与字符串转换类

    <?php /** * byte数组与字符串转化类 * @author ZT */ class Bytes { /** * 转换一个string字符串为byte数组 * @param $str ...

  3. Google Chrome: Make the Bookmarks Bar Display as Icons Only

    By reducing your bookmarks to show only the icons, you can access more of them from the Bookmarks ba ...

  4. iOS手势(滑动)返回的实现(自定义返回按钮)

    如果各位使用的是storyboard布局的话,且用的是系统的返回按钮,那么是自动会有滑动返回效果的,但是相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回 ...

  5. Dynamic CRM 2013学习笔记(三十一)自定义用excel批量导入实体数据

    有一个实体的子表数据量太大,于是客户想用execel来导入实体数据.首先想到的是用系统自带的Import Data,客户嫌太麻烦,比如lookup字段要做map等. 下面是具体的实现步骤: 一.定义e ...

  6. (转载)Autodesk面试技术题解答

    Autodesk面试技术题解答 By SmartPtr(http://www.cppblog.com/SmartPtr/)          近一年以来,AUTODESK的面试题在网上是闹的沸沸扬扬, ...

  7. [游戏学习28] MFC 时钟

    >_<:这是一个时钟小程序 >_<:通过调用获得系统时间然后经过计算得出当前时间,然后再以3个圆环表示时分秒. >_<:TAO_CLOCK.h class CMyA ...

  8. 常用linux命令索引

    每天一个linux命令(61):wget命令 每天一个linux命令(60):scp命令 每天一个linux命令(59):rcp命令 每天一个linux命令(58):telnet命令 每天一个linu ...

  9. 重新签名IPA ( iPhone )

    提示:暂时不能用了,企业证书滥用 ios 企业证书 ipa 重新签名发布 1. 应用场景 当前有一个 未用企业证书签名的 ipa 文件,默认是不可以直接安装到设备上的:我们需要用企业版证书签名: 当前 ...

  10. Javascript创建对象的学习和使用

    <html> <head> <meta charset="utf-8"> <title>javascript对象的学习</ti ...