以下代码用于检查变量类型和过滤器数组
const filter = (predicate, xs) => xs.filter(predicate)
const is = (type) => (x) => Object(x) instanceof type
filter(is(Number), [0, '1', 2, null]) // [0, 2]
Q1:我不了解Object(...)的用法,它返回什么?如果我直接使用x,代码将无效
const filter = (predicate, xs) => xs.filter(predicate);
const is = (type) => (x) => x instanceof type;
filter(is(Object), [0, '1', 2, null]) // [];
Q2:Object(...)返回的值是Object和Number / String的实例,为什么?
Object('1') instanceof String && Object('1') instanceof Object;//true
Object(2) instanceof Number && Object(2) instanceof Object;//true
typeof Object(2) //"object"
这真的让我很困惑,谢谢您的支持!