码迷,mamicode.com
首页 > 其他好文 > 详细

ES6(Decorator(修饰器))

时间:2018-05-01 12:19:09      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:有用   通过   png   重要   http   style   bsp   修饰器   class   

Decorator(修饰器)

1.基本概念

函数用来修改 类 的行为

1.Decorator 是一个函数 2.通过Decorator(修饰器)能修改 类 的行为(扩展 类 的功能)3.Decorator(修饰器)只在类的范围有用

2.基本用法

见代码

 

一、Decorator(修饰器)定义

技术分享图片

结果

技术分享图片

修改 time 属性

技术分享图片

结果:

技术分享图片

(不允许修改,达到“只读”的功能)

 2.类外面进行操作(必须在 class 前面)

技术分享图片

结果:

技术分享图片

第三方库:

技术分享图片

二、实例(卖点系统)

1.

技术分享图片

2.实例化

技术分享图片

3.运行

技术分享图片

结果:

技术分享图片

好处:1.将卖点系统抽离出来成为一个可复用的模块,复用性提高

           2.业务代码的简介度和可维护性提高

这也是Decorator(修饰器)重要的作用

 

ES6(Decorator(修饰器))

标签:有用   通过   png   重要   http   style   bsp   修饰器   class   

原文地址:https://www.cnblogs.com/jianghao233/p/8975706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!