博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] Decorators in JavaScript
阅读量:4704 次
发布时间:2019-06-10

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

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

// High order fucntionfunction 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

 

转载于:https://www.cnblogs.com/Answer1215/p/5582623.html

你可能感兴趣的文章
Java中实现String.padLeft和String.padRight
查看>>
winCVS 使用的一个小要点
查看>>
一个关于session的问题
查看>>
加快开发时间的8个CSS的预处理程序
查看>>
dom元素高度、屏幕高度 获取
查看>>
asp.net 设置session失效时间
查看>>
杭电多校第四场 E Matrix from Arrays
查看>>
ReactiveCocoa操作方法-线程\时间
查看>>
oracle 分析函数
查看>>
CHD-5.3.6集群上sqoop安装
查看>>
解决无/var/log/messages 问题
查看>>
js 判断是不是空、值是否存在
查看>>
分布式一致性协议-2PC与3PC(二)
查看>>
SCP-bzoj-1079
查看>>
Python 实践项目 游戏
查看>>
AJAX--Jquery
查看>>
模拟新浪微博随便看看
查看>>
环境搭建
查看>>
解密EXL
查看>>
简易版cnlog
查看>>