mobx学习笔记03——mobx基础语法(decorator修饰器)

在声明阶段实现类与类成员注解的一种语法。

function log(target){
 const desc = Object.getOwnPropertyDescriotors(target.prototype);
 for(const key of Object.keys(desc)){
  if(key === 'constructor'){
   continue;
  }
  const func = desc[key].value;
  if ('function' === typeof func) {
   Object.defineProperties(target.prototype,key,{
    value(...args){
     console.log('before ' + key);
     const ret = func.apply(this,args);
     console.log('after ' + key);
     return ret;
    }
   })
  }
 }
}
function readonly(target,key,descriptor){
 descriptor.writable = false;
}
function validate(target,key,descriptor){
 const func = descriptor.value;
 descriptor.value = function(...args){
  for (let num of args) {
   if ('number' !== typeof num) {
    throw new Error(`"${num}" is not a number`);
   }
  }
  return func.apply(this,args);
 }
}
@log
class Numberic{
 @readonly PI = 3.1415926;
 add(...nums){
  return nums.reduce((p,n) => (p + n),0)
 }
}

// new Numberic().add(1,2);
new Numberic().add(1,'x'); 

和上一节报同样的错误。。。

 

解决方法:主要还是插件的版本和配置文件编写的问题,要对应上不同版本的写法。

 

https://www.cnblogs.com/superjishere/p/12096419.html

 

posted @ 2019-12-24 15:23  阿江是个程序猿  阅读(414)  评论(0编辑  收藏  举报