Decorator 浅析与实践

栏目: JavaScript · 发布时间: 7年前

内容简介:在面向对象(OOP)的设计模式中,Decorator 被称为装饰模式。OOP 的装饰模式需要通过继承和组合来实现。通过装饰器动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活;它允许向一个现有的对象添加新的功能,同时又不改变其结构。Javascript 中的 Decorator 源于 python 之类的语言。

在面向对象(OOP)的 设计模式 中,Decorator 被称为装饰模式。OOP 的装饰模式需要通过继承和组合来实现。

通过装饰器动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活;它允许向一个现有的对象添加新的功能,同时又不改变其结构。

Javascript 中的 Decorator 源于 python 之类的语言。

A Python decorator is a function that takes another function, extending the behavior of the latter function without explicitly modifying it.
def decorator(func):

    print "i am decorator"
    return func

def foo():
    print('i am foo')

foo = decorator(foo)  
foo()                  

# 语法糖

@decorator
def foo():
    print("i am foo")

foo()

复制代码

这里的 @decorator 就是装饰器,利用装饰器给目标方法执行前打印出 "i am decorator" ,并且并没有对原方法做任何的修改。

Javascript 中的 Decorator

Class

es6 中的 class 其实是 Object.defineProperty 的语法糖,代码如下:

class Shopee {
    isWho() {
        console.log('One of the largest e-commerce companies in Southeast Asia')
    }
}


function Shopee() {}
Object.defineProperty(Shopee.prototype, "isWho", {
    value: function() { console.log("One of the largest e-commerce companies in Southeast Asia"); },
    enumerable: false,
    configurable: true,
    writable: true
});
复制代码

Decorator

在 es6 中的 Decorator 可以用来装饰 类 || 类方法 || 类属性

修饰类

function isAnimal(target) {
    target.isAnimal = true;
  	return target;
}

@isAnimal
class Cat {
    ...
}

console.log(Cat.isAnimal);    // true

复制代码

其实以上代码和 Cat = isAnimal(function Cat() { ... }); 基本等同。

那么当一个类有多个装饰器是怎么样的呢?

function dec_1() {
  console.log(1);
}
function dec_2() {
  console.log(2);
}
@dec_1
@dec_2
class Target {}

// 2 1
复制代码

执行顺序是 dec_2 -> dec_1

修饰类属性 || 类方法

我们利用修饰器使该方法不可读

function readonly(target, name, descriptor) {
    discriptor.writable = false;
    return discriptor;
}

class dog {
    @readonly
    say() {
        console.log("wang,wang,wang ~");
    }
}

var honey = new dog();

honey.say = function() {
    console.log("miao,miao,miao ~");
}

honey.say()    // wang,wang,wang ~
复制代码

代码中 readonly 的形参和 Object.defineProperty 的属性值一致

Object.defineProperty(object, propertyname, descriptor)
复制代码

descriptor 对象具有以下属性:

Decorator 浅析与实践

一个完整的 Decorator 具备以下特质

/**
 * 装饰者
 * @param  {Object} 类为实例化的工厂类对象
 * @param  {String} name   修饰的属性名
 * @param  {Object} desc   描述对象
 * @return {descr}        返回一个新的描述对象
 */
function decorator(target,name,desc){
}
复制代码
Decorators make it possible to annotate and modify classes and properties at design time.

A decorator is:

  • an expression , that evaluates to a function
  • that takes the target, name, and decorator descriptor as arguments
  • and optionally returns a decorator descriptor to install on the target object

装饰器允许在类和方法定义的时候去注释或者修改它。

装饰器是一个作用于函数的表达式,

它接收三个参数 target、 name 和 descriptor ,

然后可选性的返回被装饰之后的 descriptor 对象。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web容量规划的艺术

Web容量规划的艺术

阿尔斯帕瓦 / 叶飞、罗江华 / 机械工业出版社 / 2010-1 / 29.00元

《Web容量规划的艺术》由John Allspaw(F订ickr的工程运营经理)撰写,结合了他个人在F1ickr成长过程中的许多经历和很多其他产业中同行的洞察力。在衡量增长、预测趋势、成本效益等方面,他们的经验都会给你一些可靠并有效的指导。 网站的成功是以使用和增长来衡量的,而且网站类公司的成败(生死)是依赖于他们是否有能力来衡量决定他们的基础结构,从而适应不断增长的需求。作者通过自身实践给......一起来看看 《Web容量规划的艺术》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具