ES6装饰器Decorator基本用法

1. 基本形式

@decorator
class A {}

// 等同于

class A {}
A = decorator(A);

装饰器在javascript中仅仅可以修饰类和属性,不能修饰函数。
装饰器对类的行为的改变,是代表编译时发生的,而不是在运行时。
装饰器能在编译阶段运行代码。
装饰器是经典的AOP模式的一种实现方式。

2. 装饰器的执行顺序

同一处的多个装饰器是按照洋葱模型,由外到内进入,再由内到外执行

function dec(id){
    console.log(‘evaluated‘, id);
    return (target, property, descriptor) => console.log(‘executed‘, id);
}

class Example {
    @dec(1)
    @dec(2)
    method(){}
}
// evaluated 1
// evaluated 2
// executed 2
// executed 1

3. 常见的装饰器的例子

1. 类可测试,添加一个属性

@testable
class MyTestableClass {
  // ...
}

function testable(target) {
  target.isTestable = true;
}

MyTestableClass.isTestable // true

若要进行更多的配置,可以使用高阶函数,增加参数,相当于一个工厂方法,用于生产特定类型的装饰器,例如:

//testable是一个Factory
function testable(isTestable) {
  return function(target) {
    target.isTestable = isTestable;
  }
}

@testable(true)
class MyTestableClass {}
MyTestableClass.isTestable // true

@testable(false)
class MyClass {}
MyClass.isTestable // false

2. 属性readonly装饰器

class Person {
  @readonly
  name() { return `${this.first} ${this.last}` }
}

function readonly(target, name, descriptor){
  // descriptor对象原来的值如下
  // {
  //   value: specifiedFunction,
  //   enumerable: false,
  //   configurable: true,
  //   writable: true
  // };
  descriptor.writable = false;
  return descriptor;
}

3. 日志装饰器

class Math {
  @log
  add(a, b) {
    return a + b;
  }
}

function log(target, name, descriptor) {
  var oldValue = descriptor.value;

  descriptor.value = function() {
    console.log(`Calling "${name}" with`, arguments);
    return oldValue.apply(null, arguments);
  };

  return descriptor;
}

const math = new Math();

// passed parameters should get logged now
math.add(2, 4);

3. 实现memoize,备用录模式

class Person {
  @memoize
  get name() { return `${this.first} ${this.last}` }
  set name(val) {
    let [first, last] = val.split(‘ ‘);
    this.first = first;
    this.last = last;
  }
}

let memoized = new WeakMap();
function memoize(target, name, descriptor) {
  let getter = descriptor.get, setter = descriptor.set;

  descriptor.get = function() {
    let table = memoizationFor(this);
    if (name in table) { return table[name]; }
    return table[name] = getter.call(this);
  }

  descriptor.set = function(val) {
    let table = memoizationFor(this);
    setter.call(this, val);
    table[name] = val;
  }
}

function memoizationFor(obj) {
  let table = memoized.get(obj);
  if (!table) { table = Object.create(null); memoized.set(obj, table); }
  return table;
}   

参考:https://www.cnblogs.com/goloving/p/8001530.html
     https://www.cnblogs.com/whitewolf/p/details-of-ES7-JavaScript-Decorators.html

原文地址:https://www.cnblogs.com/mengff/p/9661648.html

时间: 2024-07-30 05:16:43

ES6装饰器Decorator基本用法的相关文章

Python装饰器的另类用法

之前有比较系统介绍过Python的装饰器,本文算是一个补充.今天我们一起探讨一下装饰器的另类用法. 语法回顾 开始之前我们再将Python装饰器的语法回顾一下. @decorate def f(...): pass 等同于: def f(...): pass f = decorate(f) @语法的好处在于: 相同的函数名只出现一次,避免了f = decorate(f)这样的语句. 可读性更高,让读代码的人一眼就明白函数被装饰了哪些功能. @call()装饰器 假设你要创建一个整数平方的列表,你

对Python中装饰器(Decorator)的理解与进阶

有时候我们项目中的某些功能做些修改即需要对内部的某些函数添加一些附加功能,但是为了安全起见不想改变函数的源代码以及函数的调用方式,那么装饰器在这个地方会给我们带来很大的帮助. 装饰器(Decorator):(又叫语法糖) 定义:本质是函数,功能(装饰其它函数)就是为其他函数添加附加功能 原则:(1).不能修改被装饰的函数的源代码 (2).不能修改被装饰的函数的调用方式 1.先来实现一个简单的装饰器示例: #!/usr/bin/env python # -*- coding:utf-8 -*- #

python装饰器(decorator)

最近在自学Python,在装饰器这里迷惑了我很久,有几个问题一直困惑着我.1.装饰器的语法原理:2.为什么要用装饰器: 首先来看一下装饰器的原理.总而言之,装饰器就是函数或者类作为函数的返回值.将函数或者类作为装饰器的参数传递之后,装饰器对该函数或者类进行相应操作后再将其返回.这就是装饰器的基本原理.举个例子来讲,我们定义装饰器decorator,定义函数f.其过程便如下:f=decorator(f).这里显得很抽象,我们用具体的例子来描述一下其意思.这里我们定义了一个函数: 1 def rec

请设计一个有参装饰器decorator,它可作用于任何函数上

最近有小伙伴面试,遇到一个很有意思的题:请设计一个有参装饰器decorator,它可作用于任何函数上,要求可以接受一个int作为参数,该参数为要求的执行秒数,如果该函数的执行时间大于规定的执行秒数,请打印改函数名字和执行时间. 这个题我之前面试的也遇到过,当时用高阶函数的方式实现,现在又遇到了,废话少说,咱们开发. 先来分析下: 1 有参数的装饰器 --> 高阶函数 2 执行时间 --> time 库 上代码: from functools import wraps import time #

关于Python的装饰器 decorator

语法格式是固定的:先定义一个函数,再使用@语法调用该函数. 例子一: import functools # 定义装饰器,固定格式 def log(func): @functools.wraps(func) # 将func的一些属性赋予wrapper,如__name__ def wrapper(*args, **kw): print('before function %s' % func.__name__) func(*args, **kw) print('after function %s' %

Python装饰器(Decorator)简介

Python有许多出色的语言特性,装饰器(Decorator)便是其中一朵奇葩.先来看看一段代码: def deco1(f): print 'decorate 1' return f def deco2(f): print 'decorate 2' return f @deco1@deco2 def foo(): return 'hello' 保存并执行上面的代码,你会看到如下输出: decorate 2 decorate 1 函数foo没有被调用,但是deco1,deco2被按照一个顺序被调用

就谈个py 的装饰器 decorator

很早很早就知道有这么个 装饰器的东西,叫的非常神秘. 包括c#  和 java 中都有这个东西, c#中叫做attribut 特性,java中叫做Annotation 注解,在偷偷学习c#教程的时候,我承认我 看了盗版的视频,上边的讲师一带而过,说过这个东西,所以我觉得这个东西很是高达上,这里不说这两个语言,就单说python python中实现这个 很简单,当然如果支持这种语法的话,方式是一样的. 很多程序员就喜欢把简单的东西弄复杂了,例如我,说了 这么一大堆,还没到正题,只是我要讲起来这个东

Python笔记之装饰器(decorator)

python 中一切皆对象,变量是对象,函数也是对象 def  funcName(): return 1 f = funcName                #funcName 是一个函数名,也是一个对象的名字,所以也可以赋值给一个变量,将对象funcName赋值给变量f f()   #  通过变量f来实现函数funcName函数的调用执行,f()=funcName() 装饰器本质上也是一个函数,作用是可以不修改原有代码的基础上去扩展现有函数功能.它的原理就是讲原函数对象作为参数传递给装饰器

My.Ioc 代码示例——谈一谈如何实现装饰器 (Decorator) 模式,兼谈如何扩展 My.Ioc

装饰器模式体现了一种“组合优于继承”的思想.当我们要动态为对象增加新功能时,装饰器模式往往是我们的好帮手. 很多后期出现的 Ioc 容器都为装饰器模式提供了支持,比如说 Autofac.在 My.Ioc 中,默认不提供装饰器支持,但我们可以自己进行扩展,以提供此项功能. using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using My.Ioc; u