3
var obj = {};
var fn = function(){};
obj.prop = "some value";
fn.prop = "some value";
assert( obj.prop == fn.prop, "Both are objects, both have the property." );
assert(typeof(obj) === 'object', "Yes its an object");
assert(typeof(fn) === 'object', "why is this not an object");

I heard from some people around that functions are objects and this is what i am believing so far, but why is the first condition passes well and third one fails.

1
  • Yes, yes they are. Now, what is the result of typeof fn? Commented Dec 27, 2012 at 0:09

2 Answers 2

8

That's because the direct type of a function is "function".

However, you missed this assertion:

fn instanceof Object // true

Btw, types such as "number" and "string" are strictly not descendants of Object, even though they are like objects in the sense that they have methods; just one of those things that makes JavaScript interesting :)

See also: typeof and its range of values.

Sign up to request clarification or add additional context in comments.

Comments

8

Functions are objects, but they are a particular type of object. typeof(fn) should evaluate to "function", which is a sub-type of "object".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.