zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    iOS学习05C语言函数
    iOS学习04C语言数组
    iOS学习03C语言循环结构
    iOS学习02C语言分支结构
    iOS学习01C语言数据类型
    Objective-C学习——中文URL编码和解码
    Objective-c 字面量
    SDWebImage
    mac的svn之cornerstone简易教程
    javascript 和oc交互
  • 原文地址:https://www.cnblogs.com/superjishere/p/12091734.html
Copyright © 2011-2022 走看看