这段代码会造成误解吧
if (typeof props.children === "object" && props.children.type) {
mount(props.children, dom);
}
typeof
没法区分数组和对象,起作用的还是后面的 props.children.type
, children
是数组的话props.children.type
值为空
typeof [1,2,3,4,5] // "object"
typeof {a: 1} // "object"
如果对 typeof
了解不深的人,会被带坑里去吧
应该这样写比较好吧
if (props.children.constructor === Object && props.children.type){
// ...
}
if (Object.prototype.toString.call(props.children) === "[object Object]" && props.children.type){
// ...
}