Why Arrays and Functions are Objects?

Asked

Viewed 144 times

9

I learned that objects store properties and methods:

let objeto = {
  propriedade: "valor da propriedade",
  metodo: function() {
    return "retorno"
  }
}

But I see sites calling functions and arrays of "objects" and I would like to know why.

  • Function being called object in what context? If you give typeof objeto.metodo the return will be function.

  • has an answer on the site but, can not find

  • On the mozzila:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#:~:text=In%20JavaScript%2C%20fun%C3%A7%C3%B5es%20s%C3%A3o%20objects, Guia%20de%20JavaScript%20about%20fun%C3%A7%C3%B5es. says the following: "In Javascript, functions are first-class objects, as they can have properties and methods like any other object. What differs them from other objects is that functions can be called. In short, they are Function objects." This is where I get confused, as you said yourself typeof shows "Function" and not "Object"

  • 2

    If you go up the prototyping chain, you will always reach an object. typeof Function.prototype.__proto__ === "object". This is a language design decision. With the exception of primitive types, the others derive from Object.

1 answer

5


According to the language specification:

An Object is logically a Collection of properties.

Which, in free translation, means that an object is, from the logical point of view, a collection of properties.

Thus, it can be said that, in Javascript, any value capable of possessing properties is an object. This is not the case for primitives (undefined, null, boolean, number, bigint, string and symbol).

This another answer explains why you can still access properties in primitive values. In short, it is due to the concept of Primitive wrapping.


Therefore, a function is an object because it is a structure capable of containing properties (unlike the primitives, who need the Primitive wrapping for this). In addition, the job documentation also makes it clear that a function is an object:

Every Javascript function is actually an object Function.

Already one array is nothing more than an object whose properties enumerable are numeric. It also has a nonenumerable property length - which determines its length -, in addition to "inheriting" the properties of Array.prototype. Formally, the array is described by the specification as a exotic Object.


In relation to the operator typeof, one array will always be seen as "object". However, a function, although also an object, will be considered "function". This behavior is expected, since "function" is returned by this operator for objects that have the internal property [[Call]] defined (which is the case for functions).

// Primitivos
console.log(typeof undefined); // undefined
console.log(typeof null); // object
console.log(typeof true); // boolean
console.log(typeof 123); // number
console.log(typeof 123n); // bigint
console.log(typeof 'str'); // string
console.log(typeof Symbol('foo')); // symbol

console.log('---');

// Objetos
console.log(typeof {}); // object
console.log(typeof []); // object
console.log(typeof alert); // function

You may have noticed that typeof null is "object". Although null is a primitive, the description of null for Ecmascript is "the internal absence of any object value". Some say that this is a bug (in the first version of the language). Others say it was a choice by the creator of the language, Brendan Eich. Anyway, to maintain compatibility between browsers, the behavior had to be maintained. [Reference.]

  • In fact the justification for typeof null was object is the compatibility with legacy codes. This is in fact considered a language bug, which will probably not be fixed because many codes make use of this bug, and changing its behavior would break them.

  • @user140828, in fact, this justification enters a certain debate. This answer in Stackoverflow comments on this a little more in detail. Anyway, I edited this part in the reply, thanks for the remark. :-)

  • 2

    "to maintain browser compatibility", is there the justification of a lot of "strange" thing in Javascript

Browser other questions tagged

You are not signed in. Login or sign up in order to post.