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
ES6装饰器Decorator基本用法的更多相关文章
- es6 装饰器decorator的使用 +webpack4.0配置
decorator 装饰器 许多面向对象都有decorator(装饰器)函数,比如python中也可以用decorator函数来强化代码,decorator相当于一个高阶函数,接收一个函数,返回一个被 ...
- Python装饰器的另类用法
之前有比较系统介绍过Python的装饰器(请查阅<详解Python装饰器>),本文算是一个补充.今天我们一起探讨一下装饰器的另类用法. 语法回顾 开始之前我们再将Python装饰器的语法回 ...
- Python装饰器的高级用法(翻译)
原文地址 https://www.codementor.io/python/tutorial/advanced-use-python-decorators-class-function 介绍 我写这篇 ...
- Python函数装饰器原理与用法详解《摘》
本文实例讲述了Python函数装饰器原理与用法.分享给大家供大家参考,具体如下: 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值 ...
- python 装饰器(decorator)
装饰器(decorator) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 装饰器(decorator)是一种高级Python语 ...
- python语法32[装饰器decorator](转)
一 装饰器decorator decorator设计模式允许动态地对现有的对象或函数包装以至于修改现有的职责和行为,简单地讲用来动态地扩展现有的功能.其实也就是其他语言中的AOP的概念,将对象或函数的 ...
- Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析
装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...
- Python_高阶函数、装饰器(decorator)
一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...
- python 语法之 装饰器decorator
装饰器 decorator 或者称为包装器,是对函数的一种包装. 它能使函数的功能得到扩充,而同时不用修改函数本身的代码. 它能够增加函数执行前.执行后的行为,而不需对调用函数的代码做任何改变. 下面 ...
随机推荐
- spring + mybatis 注解 @Transactional失效
1.问题 在使用@Transactional注解管理事务的时候会出现很多错误,比如: *** was not registered for synchronization because synchr ...
- jmeter多台压力机测试
jmeter控制机会自动将脚本发送至压力机 1.控制机配置 jmeter.properties中配置: remote_hosts=ip1:1099,ip2:1022,ip3:1099 将压力机ip+p ...
- 获取ip地址以及获取城市等信息
class Program { static void Main(string[] args) { string ip = GetIP(); if (ip != null) { string city ...
- Unity热更新文件的服务器部署(IIS)
1.VS新建一个"ASP.NET空网站" 工程结构如下 最好设置.Net FrameWork版本为 V4.0或者V4.5版本的,因为我们的程序最后是要部署到阿里云的虚拟服务器上的, ...
- 【转】APP推广什么是cpa,cps,cpm
转载自:http://www.apptg.cn 经常做做APP推广和做运营的同学对于cpa,cps,cpm,cpc这些名词肯定不会陌生,也基本都知道其表示的含义,但是对于新手来说,这几个词的含义还是不 ...
- 【system.file】使用说明
对象:system.file 说明:提供一系列针对文件操作的方法. 注意:参数中的filePath 均为相对网站根目录路径 目录: 方法 返回 说明 system.file.exists(filePa ...
- JDK源码分析:Short.java
Short是基本数据类型short的包装类. 1)声明部: public final class Short extends Number implements Comparable<Short ...
- n! 阶乘
其实1.2.3.4.6.7…都是可以不用考虑的,因此选择以5为迭代步数即可. 首先,这些数字都可以不用进行%5(对5取余数)运算,因此每次循环时可以直接将函数的count变量直接加1.其次,考虑25. ...
- C语言 命令行参数 函数指针 gdb调试
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...
- java中多种方式读文件
转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...