为对象添加功能,不改变其原有的结构和功能
,类似手机壳,手机功能还是正常使用,但是加了手机壳,可以防滑,美观
class Circle{
draw(){
console.log('画图')
}
}
class Decorator{
constructor(circle){
this.circle=circle;
}
draw(){
this.circle.draw();
}
setRedBorder(){
this.circle.color='给图加红色的边框';
console.log(this.circle.color);
}
}
class Client{
constructor(){
/*原始的circle*/
this.circle1=new Circle();
/*使用装饰器生成的circle*/
this.circle2=new Decorator(new Circle());
}
main(){
this.circle1.draw();
this.circle2.draw();
this.circle2.setRedBorder();
}
}
const client=new Client();
client.main();
场景:
ES7装饰器
core-decorators:装饰类,装饰方法(插件babel-plugin-transform-decorators-legacy)
来源:CSDN
作者:aliven1
链接:https://blog.csdn.net/aliven1/article/details/103855415