Decorator 浅析与实践

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

内容简介:在面向对象(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 对象。


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

查看所有标签

猜你喜欢:

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

JAVA语言规范(英文版,第3版)

JAVA语言规范(英文版,第3版)

戈斯林 / 机械工业 / 2006-4 / 79.00元

本书由“java之父”Jame Gosling 以及另外三位顶级大师撰写而成,无论是对java语言的初学者还是专业程序员都具有极高的价值,是关于java程序设计语言最权威的技术参考书。   本书侧重于java技术细节和内幕,全面,准确,详尽地介绍了java语言及其语法,论述了java编译器所要检查的语法和java运行模式的各个方面,同时还描述了java语言最重要的新特征。一起来看看 《JAVA语言规范(英文版,第3版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具