25

The MDN said :

Six data types that are primitives:

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol (new in ECMAScript 6)

  • and Object

But I confused, the function data type and object data type.

Let's see :

var func = function() { 
    console.log ('Hello World ! ')
};

var obj = { 
    property : something
}  


console.log(typeof(func)); // ===> function
console.log(typeof(obj)); // ===> object 

Is it different function data type and object data type? Why typeof(func) is function? not a object? The document said there are 7 data type (6 primitive, 1 object). function is not include anywhere.

Until now, over 1 year, I think function's data type is object, I heard the function is first class object in JavaScript, so I don't have doubt about function is object but today I think more time, and wondered.

Is it different?

4
  • 2
    It returns function but its a function object developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 3, 2016 at 19:30
  • 2
    typeof doesn't return the Data Type. Commented Feb 3, 2016 at 19:36
  • 2
    typeof doesn't report the value's true data type. It's a lookup table that maps data type and other value characteristics to a name: ecma-international.org/ecma-262/6.0/… . Functions are objects that have an internal [[Call]] property. That's why typeof null is "object" (*grumpy face*). Commented Feb 3, 2016 at 19:37
  • note the particular mapping of typeof null to "object" is historical Commented Jun 21, 2024 at 5:33

2 Answers 2

18

You can logically think of Function as a subclass of Object. It has all the methods of Object plus some more that are specific to a function (such as .bind(), .call(), .apply(), etc...).

Why Javascript decided to make Function report it's own unique type, but not Array (which is a similar derivation from Object) is anyone's guess and probably only known to the original designers of the language. It is extremely useful that Function does report its own type so you can easily check if a property is callable as a function and perhaps that is the main reason why it was done this way.

Here's a demonstration of how a Function object has the methods from an Object:

function f() {}
f.someProperty = "foo";

log(f.hasOwnProperty("someProperty"));
log(f instanceof Object);
log(f instanceof Function);

function log(x) {
    var div = document.createElement("div");
    div.innerHTML = x;
    document.body.appendChild(div);
}

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

7 Comments

Where would Object.create(null) fit in this class hierarchy?
functions are not characterised by being instanceof Function in JavaScript, they are special because they are callable objects.
@Bergi - But, a function is instanceof Function. Object.create(null) is a bit of an odd beast since it has no prototype. One could think of it as the parent of Object, I suppose since it offers the common property accessor syntax.
Yeah, most functions you encounter are Functions, but they don't have to be. They could come from another realm (frame), could have their [[prototype]] manipulated or just be host objects with a buggy implementation (oldIE's console.log being well known for this - these days, browser vendors care to minimise wtf moments)
@Bergi - So, now you're off description some exceptions. instanceof doesn't work for all objects in another frame - that's nothing new for a function object, same issue with instanceof Array for example. And, your IE comment sounds like it's a bug. Is there something you think I should correct or improve in my answer?
|
4

typeof returns the type of what ever is passed to it. A function is an object ((function () {}) instanceof Object === true), but the typeof function is defined to return "function" when an Object implements [[Call]] in ECMA-262 is supplied to it.

Functions are objects, but typeof treats them as a special case.

3 Comments

"when an Object with a call method is supplied to it" - I beg to differ. typeof {call:function(){}} returns object.
One might assume that you mean the special internal [[call]] method, but the wording is confusing indeed
@bergi Yeah, I changed the wording for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.