JavaScript装饰器的实现原理详解

Sally ·
更新时间:2024-09-20
· 630 次阅读

目录

装饰器的常见作用

装饰类的属性

装饰类

注意

实例应用

最近在使用TS+Vue的开发模式,发现项目中大量使用了装饰器,看得我手足无措,今天特意研究一下实现原理,方便自己理解这块知识点。

装饰器的常见作用

装饰一个类的属性

装饰一个类

装饰器只能针对类和类的属性,不能直接作用于函数,因为存在函数提升。

下面我们针对这两种情况进行举例阐述。

装饰类的属性 function readonly(target, name, descriptor) { discriptor.writable = false; return discriptor; } class Cat { @readonly say() { console.log("meow ~"); } } var kitty = new Cat(); kitty.say = function() { console.log("woof !"); } kitty.say() // meow ~

ES6中的类实际上就是一个语法糖,本质上是构造函数,类的属性的定义使用的是 Object.defineProperty() 类的代码可以改写为下面代码:

class Cat { say() { console.log("meow ~"); } } // 上面等价于下面代码 function Cat() {} Object.defineProperty(Cat.prototype, "say", { value: function() { console.log("meow ~"); }, enumerable: false, configurable: true, writable: true });

say方法前面加入@readonly后,整个代码等价于下面代码:

function readonly(target, name, descriptor) { discriptor.writable = false; return discriptor; } function Cat() {} let descriptor = { value: function() { console.log("meow ~"); }, enumerable: false, configurable: true, writable: true }; descriptor = readonly(Cat.prototype, 'say', descriptor) || descriptor; Object.defineProperty(Cat.prototype, "say", descriptor);

装饰类属性的大致实现原理,就是上面的语法糖实现过程。

这里注意下装饰器的用法,是在属性前面加入@readonly,不是@readonly()。如果是后一种写法的话,装饰器函数需要返回一个函数来表示装饰器。这种写法主要是通过传参来扩展装饰器函数的功能,使装饰器函数复用性更强。

如通过传参决定是否设置只读,代码如下:

function readonly(flag) { return function (target, name, descriptor) { if (flag) { discriptor.writable = false; } else { discriptor.writable = true; } return discriptor; } } class Cat { @readonly(true) say() { console.log("meow ~"); } } var kitty = new Cat(); kitty.say = function() { console.log("woof !"); } kitty.say() 装饰类

装饰一个类的时候类本身本质上是一个函数,没有descriptor,target是这个函数本身。

function isAnimal(target) { target.isAnimal = true; return target; } @isAnimal class Cat { ... } console.log(Cat.isAnimal); // true

也就是说,上面的@isAnimal其实就是做了下面这件事

Cat = isAnimal(function Cat() { ... }); 注意

作用在方法上的 decorator 接收的第一个参数(target )是类的 prototype;如果把一个 decorator 作用到类上,则它的第一个参数 target 是 类本身。

装饰器执行的时间是在属性定义的时候,也就是被装饰的属性在定义后就是已经被装饰器处理过的不一样的属性了。

下面举例看下装饰器的执行时间:

function log(message) { return function() { console.log(message); } } console.log('before class'); @log('class Bar') class Bar { @log('class method bar'); bar() {} @log('class getter alice'); get alice() {} @log('class property bob'); bob = 1; } console.log('after class'); let bar = { @log('object method bar') bar() {} };

输出结果:

before class
class method bar
class getter alice
class property bob
class Bar
after class
object method bar

可以看出装饰器在类和类的属性定义的时候就对它们进行了"装饰"。

实例应用

我们的示例场景是这样的

首先创建一个普通的Man类,它的抵御值 2,攻击力为 3,血量为 3;

然后我们让其带上钢铁侠的盔甲,这样他的抵御力增加 100,变成 102;

让其带上光束手套,攻击力增加 50,变成 53;

最后让他增加“飞行”能力

1. 创建 Man 类:

class Man{ constructor(def = 2,atk = 3,hp = 3){ this.init(def,atk,hp); } init(def,atk,hp){ this.def = def; // 防御值 this.atk = atk; // 攻击力 this.hp = hp; // 血量 } toString(){ return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`; } } var tony = new Man(); console.log(`当前状态 ===> ${tony}`); // 输出:当前状态 ===> 防御力:2,攻击力:3,血量:3

2. 装饰器为钢铁侠装配盔甲:

function decorateArmour(target, key, descriptor) { const method = descriptor.value; let moreDef = 100; let ret; descriptor.value = (...args)=>{ args[0] += moreDef; ret = method.apply(target, args); return ret; } return descriptor; } class Man{ constructor(def = 2,atk = 3,hp = 3){ this.init(def,atk,hp); } @decorateArmour init(def,atk,hp){ this.def = def; // 防御值 this.atk = atk; // 攻击力 this.hp = hp; // 血量 } toString(){ return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`; } } var tony = new Man(); console.log(`当前状态 ===> ${tony}`); // 输出:当前状态 ===> 防御力:102,攻击力:3,血量:3

3. 装饰器增加光束手套

function decorateLight(target, key, descriptor) { const method = descriptor.value; let moreAtk = 50; let ret; descriptor.value = (...args)=>{ args[1] += moreAtk; ret = method.apply(target, args); return ret; } return descriptor; } class Man{ constructor(def = 2,atk = 3,hp = 3){ this.init(def,atk,hp); } @decorateArmour @decorateLight init(def,atk,hp){ this.def = def; // 防御值 this.atk = atk; // 攻击力 this.hp = hp; // 血量 } ... } var tony = new Man(); console.log(`当前状态 ===> ${tony}`); //输出:当前状态 ===> 防御力:102,攻击力:53,血量:3

在这里你就能看出装饰模式的优势了,它可以对某个方法进行叠加使用,对原类的侵入性非常小,只是增加一行@decorateLight而已,可以方便地增删;(同时还可以复用)

4. 增加飞行能力

function addFly(canFly){ return function(target){ target.prototype.canFly = canFly; let extra = canFly ? '(技能加成:飞行能力)' : ''; let method = target.prototype.toString; target.prototype.toString = (...args)=>{ return method.apply(target.prototype,args) + extra; } return target; } } @addFly(true) class Man{ constructor(def = 2,atk = 3,hp = 3){ this.init(def,atk,hp); } @decorateArmour @decorateLight init(def,atk,hp){ this.def = def; // 防御值 this.atk = atk; // 攻击力 this.hp = hp; // 血量 } ... } ... console.log(`当前状态 ===> ${tony}`); // 输出:当前状态 ===> 防御力:102,攻击力:53,血量:3(技能加成:飞行能力)

到此这篇关于JavaScript装饰器的实现原理详解的文章就介绍到这了,更多相关JavaScript装饰器内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



JavaScript

需要 登录 后方可回复, 如果你还没有账号请 注册新账号