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

后端 未结 5 1980
野的像风
野的像风 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:33

    it functions like an array, and for all purposes, it is an array

    No. It has no auto-updating length property. See this article why it's quite impossible to subclass Array.

    Is there any way to figure out if an object inherits from a particular prototype? I suppose you could iterate through the prototypes, but it feels a tad hacky.

    That's just how to do it. A cleaner approach than a self-written function would be to use the instanceof operator:

    arr instanceof Array; // true
    

提交回复
热议问题