[Javascript] Bitwise operation

export const enum ShapeFlags {
  ELEMENT = 1,
  FUNCTIONAL_COMPONENT = 1 << 1, //2
  STATEFUL_COMPONENT = 1 << 2, //4
  TEXT_CHILDREN = 1 << 3, //8
  ARRAY_CHILDREN = 1 << 4, //16
  SLOTS_CHILDREN = 1 << 5, //32
  TELEPORT = 1 << 6, //64
  SUSPENSE = 1 << 7, //128
  COMPONENT_SHOULD_KEEP_ALIVE = 1 << 8, //256
  COMPONENT_KEPT_ALIVE = 1 << 9, //512
  COMPONENT = ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT, // 6
}

/**
 *  0001
 * |1000
 * ------
 *  1001 = 9
 *
 *  00001
 * |10000
 * --------
 *  10001 = 17
 *
 *  1001
 * &0001
 * --------
 *  0001 = 1
 *
 *  1001
 * &0010
 * ------
 *  0000 = 0
 */
function createVNode(type: any, props: any, children: any) {
  const shapeFlag = isString(type) ? ShapeFlags.ELEMENT : 0;

  const vnode = {
    __v_isVNode: true,
    type,
    props,
    children,
    component: null,
    el: null,
    key: props?.key,
    shapeFlag,
  };

  if (children) {
    if (isArray(children)) {
      vnode.shapeFlag |= ShapeFlags.ARRAY_CHILDREN;
    } else {
      vnode.shapeFlag |= ShapeFlags.TEXT_CHILDREN;
    }
  }

  return vnode;
}

 

posted @ 2025-06-13 14:26  Zhentiw  阅读(14)  评论(0)    收藏  举报