『JS装饰器』vscode vue

247 阅读1分钟

背景

使用js装饰器,主要目的为了保证核心代码区尽量清晰(将非业务逻辑埋点、log这些作区分)

需要处理这4件事

  1. vscode
  2. vue2 vscode插件Vetur
  3. eslint
  4. JS使用

vscode设置

关键词:decorator image.png

vue2 vscode插件Vetur

关键词: vetur.validation.scri

image.png

eslint

image.png

JS使用

个人主要用来做js埋点

function log () {
 /**
   * @param target 对应method对象
   * @param name 对应属性方法的名称
   * @param descriptor 对应属性方法的修饰符
   */
  return function(target, name, descriptor) {
    console.warn(target, name, descriptor);
    const fn = descriptor.value;
    descriptor.value = function (...rest) {
      console.warn('1111');
      fn.call(this, ...rest);
      console.warn('2222');
    }
  };
 }

@log()
function clickSM () {
  // todo 业务逻辑
}

这篇博客中提到了更多的用法

参考blogs

blog.csdn.net/weixin_3910… blog.csdn.net/weixin_4473…