One of the unique features of JavaScript is that ‘functions are objects and can be passed around like any objects’ [1].
When Mr. Eich began creating the language, why did he decide to do this? How is it advantageous compared to the usual OOP style in languages like Java? Did he have a precedent or was this a completely unique idea at the time?
Certainly he did not invent it, first-class functions have existed since the ’60s.
The power it gives you is to write so-called higher-class functions – functions that take other functions as arguments. This is the corner-stone of functional programming. For example,
Array.mapis a function that takes another function as input, allowing powerful constructions.Object-Oriented programming was developed later, and concentrates more on coupling/separating data and behaviour. This is often at the expense of easily reasoning about the code (both in the colloquial and mathematical senses). Many OO languages (Java, C#) are now adding in these elements of functional programming (i.e. lambdas).
Consider the complexity of implementing
Array.map,Array.reduce,Array.filterin a language like Java. Every time you want to use it, you have to create an instance of a special anonymous inner class just for the purpose of implementing the function required for the algorithm to call. In JavaScript functions are just objects like everything else, so you can just pass one in, leading to much more concise and natural syntax.