1、typescript

  1. class Circle {
  2. draw() {
  3. console.log('画一个⭕️')
  4. }
  5. }
  6. class Decorator {
  7. private circle: Circle;
  8. constructor(circle: Circle) {
  9. this.circle = circle;
  10. }
  11. draw() {
  12. this.circle.draw()
  13. this.drawLine()
  14. }
  15. drawLine() {
  16. console.log('画一个直线')
  17. }
  18. }
  19. const circle = new Circle()
  20. const decorator = new Decorator(circle)
  21. decorator.draw()

2、前端里面装饰器(@)

  1. function mixins(...list) {
  2. return function (target) {
  3. Object.assign(target.prototype, ...list)
  4. }
  5. }
  6. const Foo = {
  7. foo() {
  8. console.log('foo')
  9. }
  10. }
  11. interface obj {
  12. [key: string]: any
  13. }
  14. @mixins(Foo)
  15. class Myclass {
  16. }
  17. var obj: obj = new Myclass()
  18. obj.foo()

3、装饰器方法readonly

  1. function readonly(target, name, descriptor) {
  2. descriptor.writable = false
  3. return descriptor
  4. }
  5. class Person {
  6. first: string;
  7. last: string;
  8. constructor() {
  9. this.first = 'Kip'
  10. this.last = 'song'
  11. }
  12. @readonly
  13. name() {
  14. console.log(`${this.first} ${this.last}`)
  15. }
  16. }
  17. var p = new Person()
  18. console.log(p.name())
  19. p.name = function () {
  20. alert(100)
  21. }
  22. p.name() // 还是之前的name方法

4、装饰log日志

  1. function log(target, name, descriptor) {
  2. let oldValue = descriptor.value
  3. descriptor.value = function () {
  4. console.log(`操作了 ${name} 方法`, arguments)
  5. return oldValue.apply(this, arguments)
  6. }
  7. return descriptor
  8. }
  9. class MyMath {
  10. @log
  11. add(a: number, b: number) {
  12. return a + b
  13. }
  14. }
  15. const math = new MyMath()
  16. math.add(1, 2)

5、装饰一下小程序代码(字面意思哦)

  1. export default {
  2. /**
  3. * @author Kip song
  4. * @param {*} params
  5. * @param {*} method
  6. */
  7. baseOptions(params, method = "GET") {
  8. return new Promise(function (resolve, reject) {
  9. let { url, data } = params
  10. let contentType = "application/json"
  11. contentType = params.contentType || contentType
  12. const option = {
  13. url: url.indexOf("http") !== -1 ? url : config.url + url,
  14. data: data,
  15. method: method,
  16. header: { 'content-type': contentType, 'Wx-Token': getToken() },
  17. success(res) {
  18. if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
  19. wx.hideLoading()
  20. wx.showToast({
  21. title: '服务器找不到您所请求的文件或脚本!',
  22. icon: 'none',
  23. duration: 1500
  24. })
  25. } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
  26. wx.hideLoading()
  27. wx.showToast({
  28. title: '服务端异常,请稍后尝试!',
  29. icon: 'none',
  30. duration: 1500
  31. })
  32. } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
  33. wx.hideLoading()
  34. wx.showToast({
  35. title: '没有权限访问!',
  36. icon: 'none',
  37. duration: 1500
  38. })
  39. } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
  40. let _data = res.data
  41. if (_data.code == 0) {
  42. resolve(_data)
  43. } else {
  44. wx.showToast({
  45. title: _data.msg || _data.message || `错误状态${_data.code}`,
  46. icon: 'none',
  47. duration: 1500
  48. })
  49. }
  50. } else {
  51. wx.hideLoading()
  52. wx.showToast({
  53. title: '网络出现故障,请重新尝试!',
  54. icon: 'none',
  55. duration: 1500
  56. })
  57. }
  58. },
  59. fail(e) {
  60. wx.showToast({
  61. title: '网络超时,请重新操作!',
  62. icon: 'none',
  63. duration: 1500
  64. })
  65. }
  66. }
  67. wx.request(option)
  68. })
  69. },
  70. /**
  71. * GET请求
  72. * @param url 请求路径
  73. * @param data 参数
  74. */
  75. get(url, data) {
  76. let option = { url, data }
  77. return this.baseOptions(option, "GET")
  78. },
  79. /**
  80. * POST请求
  81. * @param url 请求路径
  82. * @param data 参数
  83. */
  84. post(url, data) {
  85. let option = { url, data }
  86. return this.baseOptions(option, "POST")
  87. },
  88. /**
  89. * PUT请求
  90. * @param url 请求路径
  91. * @param data 参数
  92. */
  93. put(url, data) {
  94. let option = { url, data }
  95. return this.baseOptions(option, "PUT")
  96. },
  97. /**
  98. * DELETE请求
  99. * @param url 请求路径
  100. * @param data 参数
  101. */
  102. delete(url, data) {
  103. let option = { url, data }
  104. return this.baseOptions(option, "DELETE")
  105. }
  106. }