You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@DecclassDemo{// ...}functionDec(target){target.speak=function(){console.log("I can speak")}}Demo.speak();// I can speak
3、传参数
只需要记住返回一个function
@Dec("basketball")classDemo{// ...}functionDec(args){returnfunction(target){target.speak=function(){console.log((`I love ${args}`))}}}Demo.speak();// I love basketball
4、mixin实现
functionmixins(...list){returnfunction(target){Object.assign(target.prototype, ...list)}}constFoo={foo(){console.log("I am foo")}}
@mixins(Foo)classMyClass{}letobj=newMyClass()obj.foo();
装饰者模式
装饰者模式:在不改变对象自身的基础上,为对象动态添加行为
先看个最简单的例子
ES7的Decorator
1、ES7装饰器环境
使用babel编译
2、最简单用法
3、传参数
只需要记住返回一个
function
4、mixin实现
把
Foo
的属性和方法混入到MyClass
类,轻松实现mixins
装饰属性和方法
利用
Object.defineProperty
The text was updated successfully, but these errors were encountered: