1、typescript
class Circle { draw() { console.log('画一个⭕️') }}class Decorator { private circle: Circle; constructor(circle: Circle) { this.circle = circle; } draw() { this.circle.draw() this.drawLine() } drawLine() { console.log('画一个直线') }}const circle = new Circle()const decorator = new Decorator(circle)decorator.draw()
2、前端里面装饰器(@)
function mixins(...list) { return function (target) { Object.assign(target.prototype, ...list) }}const Foo = { foo() { console.log('foo') }}interface obj { [key: string]: any}@mixins(Foo)class Myclass {}var obj: obj = new Myclass()obj.foo()
3、装饰器方法readonly
function readonly(target, name, descriptor) { descriptor.writable = false return descriptor}class Person { first: string; last: string; constructor() { this.first = 'Kip' this.last = 'song' } @readonly name() { console.log(`${this.first} ${this.last}`) }}var p = new Person()console.log(p.name())p.name = function () { alert(100)}p.name() // 还是之前的name方法
4、装饰log日志
function log(target, name, descriptor) { let oldValue = descriptor.value descriptor.value = function () { console.log(`操作了 ${name} 方法`, arguments) return oldValue.apply(this, arguments) } return descriptor}class MyMath { @log add(a: number, b: number) { return a + b }}const math = new MyMath()math.add(1, 2)
5、装饰一下小程序代码(字面意思哦)
export default { /** * @author Kip song * @param {*} params * @param {*} method */ baseOptions(params, method = "GET") { return new Promise(function (resolve, reject) { let { url, data } = params let contentType = "application/json" contentType = params.contentType || contentType const option = { url: url.indexOf("http") !== -1 ? url : config.url + url, data: data, method: method, header: { 'content-type': contentType, 'Wx-Token': getToken() }, success(res) { if (res.statusCode === HTTP_STATUS.NOT_FOUND) { wx.hideLoading() wx.showToast({ title: '服务器找不到您所请求的文件或脚本!', icon: 'none', duration: 1500 }) } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) { wx.hideLoading() wx.showToast({ title: '服务端异常,请稍后尝试!', icon: 'none', duration: 1500 }) } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) { wx.hideLoading() wx.showToast({ title: '没有权限访问!', icon: 'none', duration: 1500 }) } else if (res.statusCode === HTTP_STATUS.SUCCESS) { let _data = res.data if (_data.code == 0) { resolve(_data) } else { wx.showToast({ title: _data.msg || _data.message || `错误状态${_data.code}`, icon: 'none', duration: 1500 }) } } else { wx.hideLoading() wx.showToast({ title: '网络出现故障,请重新尝试!', icon: 'none', duration: 1500 }) } }, fail(e) { wx.showToast({ title: '网络超时,请重新操作!', icon: 'none', duration: 1500 }) } } wx.request(option) }) }, /** * GET请求 * @param url 请求路径 * @param data 参数 */ get(url, data) { let option = { url, data } return this.baseOptions(option, "GET") }, /** * POST请求 * @param url 请求路径 * @param data 参数 */ post(url, data) { let option = { url, data } return this.baseOptions(option, "POST") }, /** * PUT请求 * @param url 请求路径 * @param data 参数 */ put(url, data) { let option = { url, data } return this.baseOptions(option, "PUT") }, /** * DELETE请求 * @param url 请求路径 * @param data 参数 */ delete(url, data) { let option = { url, data } return this.baseOptions(option, "DELETE") }}