Why is Array instanceof Object?

I played with instanceofin javascript and I came across the following.

Array instanceof Object
returns true

Object instanceof Array
returns false

What is the relationship between the array and the object here?

+4
source share
4 answers

Between the designers, the connection or prototype chain is as follows:

Array -> Function.prototype -> Object.prototype
Object -> Function.prototype -> Object.prototype

The first is truebecause the constructor is this Function, and the functions themselves are objects.

Array instanceof Function // true
Object instanceof Function // true

(function () {}) instanceof Object // true
+7
source

Array. Array - , . , Array instanceof Function , Array instanceof Object ( JS Object. -, Array instanceof Array .

Object - Object, . Object instanceof Function , Object instanceof Object.

, ( ), . ( )

[] instanceof Array [] instanceof Object ( [] instanceof Function - false). , , , .

{} instanceof Object , {} instanceof Array {} instanceof Function .

  • Array - , . - , - .

  • - , , .

  • instanceof " ", , .

+1

- , . - Javascript, , , . , , .

0

An array (first capital letter) is the name of the "class object" of all arrays in JavaScript. This "class object" is an object, therefore, instanceof Objectfor it true.

But an object of class "object" is not an array - it Objectdoes not have a class Arrayin its inheritance chain.

0
source

All Articles