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.]
Function being called object in what context? If you give
typeof objeto.metodo
the return will befunction
.– Rafael Tavares
has an answer on the site but, can not find
– novic
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"
– Alex
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.– bfavaretto