import { _flattenChildren } from "./createElement";

export const Children = {
    only(children) {
        //only方法接受的参数只能是一个对象,不能是多个对象(数组)。
        if (Array.isArray(children)) {
            children = children[0];
        }
        if (children && children.vtype) {
            return children;
        }
        throw new Error("expect only one child");
    },
    count(children) {
        return _flattenChildren(children, false).length;
    },
    forEach(children, callback, context) {
        _flattenChildren(children, false).forEach(callback, context);
    },
    map(children, callback, context) {
        return _flattenChildren(children, false).map(callback, context);
    },
    toArray: function (children) {
        return _flattenChildren(children, false);
    }
};

 

相关文章:

  • 2021-11-13
  • 2021-10-25
  • 2021-04-26
  • 2021-09-13
  • 2021-10-21
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-10
  • 2021-12-05
  • 2022-01-08
  • 2021-11-07
  • 2021-09-11
  • 2021-09-04
  • 2021-05-25
相关资源
相似解决方案