Is it possible to determine if an object created with Object.create inherits from Array in JavaScript?

后端 未结 5 1962
野的像风
野的像风 2021-01-12 12:59

Identifying which objects are which is complicated in JavaScript, and figuring out which objects are arrays has something of a hacky solution. Fortunately, it manages to wor

5条回答
  •  耶瑟儿~
    2021-01-12 13:19

    How about checking the constructor?

    function inherits(obj, proto) {
        return obj.constructor === proto.constructor;
    }
    
    inherits(Object.create(Array.prototype), Array.prototype);  // true
    

提交回复
热议问题