How is it that Functions are Objects in JavaScript?

How is it that Functions are Objects in JavaScript? Since Functions are Objects, then this redefines what a function is and it is no longer a method of a class. If Functions are Objects, then how do they differ from Objects?

That’s never been the definition of a function. Methods are Functions; functions are not necessarily methods. Squares and Rectangles.

Functions are Objects that support the Call internal method.

2 Likes

Technically speaking, they inherit from objects; just like you can do:

class Foo {}
class Bar extends Foo {}

… you might think of it like:

class function extends object {
  // [internal code]
}

And you can indeed use them like any other object, pass them around, assign properties etc…

function foo () {}
foo.bar = 42
console.assert(foo.bar === 42)
1 Like

In my opinion, JavaScript was not designed well. It sure does not do OOP well. The language has some strange and confusing features, such as what you are asking about here, that do not make sense to me.

Please do not use JavaScript to understand OOP. Do not expect JavaScript to be object-oriented. Anyone familiar with OOP can use OOP as much as possible in JavaScript but the language has limited support.

Due to idiosyncrasies such as described here, I prefer other languages. It is unfortunate that alternatives to browser-side scripting is limited, but that is getting off-topic.