1

I'm diving (or trying) into advanced javascript topics like prototypes, constructors, function properties, etc. and some facts lead me to conclusion, that javascript functions are also objects.

I know that datatype of functions is function, since:

> typeof function(){}
'function'

but anyway:

  • you can assign properties to a function (just like it was an object)
  • function has its length attribute
  • Function.prototype's prototype is Object.prototype (prototype chaining):

    > Object.getPrototypeOf(Function.prototype) === Object.prototype
    true
    

Can someone please make it clear whether javascript functions are objects? If so, why is typeof function == 'function' and typeof [] == 'object'; if not - how about the points I mentioned above?

Edit: one more question - what is a first class object?

ducin
  • 25,621
  • 41
  • 157
  • 256
  • 2
    Welcome to the wacky world of Javascript. – Jared Farrish Jun 26 '13 at 21:24
  • @ First class object: http://stackoverflow.com/questions/705173/what-is-meant-by-first-class-object – dualed Jun 26 '13 at 21:25
  • There are of course [other answers](http://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object). (And [MDN's entry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Object_everything).) – Jared Farrish Jun 26 '13 at 21:26
  • @JaredFarrish omg, looking at the other answer... it's a really wacky world... – ducin Jun 26 '13 at 21:34
  • Yes. In JavaScript functions are objects. The ES5 spec specifies [function objects](http://es5.github.io/#x15.3). The problem is, the `typeof` operator is broken :) – Benjamin Gruenbaum Jun 26 '13 at 23:32

4 Answers4

5

Everything (almost) is an object in javascript.

Function instanceof Object  // true
mpm
  • 20,148
  • 7
  • 50
  • 55
2

Yes, Functions are Objects in javascript. That's why ((function(){}) instanceof Object is true.

typeof function == 'function', because they are also functions. Functions inherit from the Object prototype.

Anything instantiated with an object initializer (object literals, array literals, new Constructor) will have the type 'object'.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • So why typeof array returns object instead of array? inconsistency? – ducin Jun 26 '13 at 21:23
  • @tkoomzaaskz There is some info here on what typeof returns: http://javascript.crockford.com/remedial.html – Paul Jun 26 '13 at 21:26
  • @tkoomzaaskz instanceof is a better way to check if an object is inheriting from the prototype of a constructor. – Paul Jun 26 '13 at 21:28
0

This part has been already addressed by other answers, but yes, functions are objects. So, on to your other questions.

why is typeof function == 'function' and typeof [] == 'object'

The typeof operator just follows a certain set of rules defined in the specification:

Type of val                    Result
----------------------------------------------
Undefined                      "undefined"
Null                           "object"
Boolean                        "boolean"
Number                         "number"
String                         "string"
Object (native and does        "object"
not implement [[Call]])        
Object (native or host and     "function"
does implement [[Call]])
Object (host and does not      Implementation-defined except may 
implement [[Call]])            not be "undefined", "boolean", 
                               "number", or "string".

what is a first class object?

It's said that functions are "first class objects" or "first class citizens" in JavaScript because, as regular objects, they can be assigned to variables, passed to other functions, and returned by other functions. That's what enables JavaScript's functional nature.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

I ran into this issue myself when learning JavaScript early on. This is a core concept of JavaScript: Functions ARE objects. However, an object is not necessarily a function. Reason? The type "function" implies a process or method that works on other objects and variables. So, instead of creating a type called "function object", they chose the name "function". This has caused further confusion even at the highest levels of programmers.

But simply said, functions are objects. So, they can also contain properties of their own (like any object). And at this point, you can create private variables/objects contained by this function and which every new instance gets. Important to note that any objects contained within a function will not have global scope. The object in "Object.prototype" has global scope however (and is always present).

To answer your last question, first-class objects are usually entities that have no restrictions in terms of how they can be passed, created or modified. The next levels (2nd, 3rd, ...) have layers of restrictions which limit their usage. There is obviously good use-cases for all of them.

EDIT: I know everyone sources him to death. But check out Douglas Crocford's teachings. He delves into advanced JS quite well.

Sri--
  • 451
  • 3
  • 6