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

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

    See http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ for the definitive exposition of the problems in inheriting from Array.

    Anyway, in the simplest case, where you are doing

    var sort_of_an_array = Object.create(Array.prototype);
    

    you can check using isPrototypeOf:

    Array.prototype.isPrototypeOf(sort_of_an_array)
    

    See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf.

提交回复
热议问题