解析多层属性,类似angularJS的$parse服务

parser.js
复制

function parse(properties) {
  const agm = properties.split('.');

  return function(target, undefined) {
    let result = null;
    (function parseTarget(target, agm) {
      if (target === void 0) {
        console.error(
          'Uncaught TypeError: Cannot read property ' + agm[0] + ' of undefined'
        );
        result = target;
        return result;
      }
      if (agm.length <= 1) {
        result = target[agm[0]];
        return result;
      }
      target = target[agm[0]];
      agm.shift();
      parseTarget(target, agm);
    })(target, agm);

    return result;
  };
}

大牛们的评论:朕有话说

还没有人评论哦,赶紧抢沙发!