最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is Function's Data Type : function or object? in JavaScript - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Oct 13, 2018 at 17:20 Damjan Pavlica 34k10 gold badges75 silver badges78 bronze badges asked Feb 3, 2016 at 19:24 ton1ton1 7,62822 gold badges80 silver badges129 bronze badges 4
  • 2 It returns function but its a function object developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Mr. Alien Commented Feb 3, 2016 at 19:30
  • 2 typeof doesn't return the Data Type. – Bergi 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*). – Felix Kling Commented Feb 3, 2016 at 19:37
  • note the particular mapping of typeof null to "object" is historical – shea Commented Jun 21, 2024 at 5:33
Add a comment  | 

2 Answers 2

Reset to default 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);
}

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.

发布评论

评论列表(0)

  1. 暂无评论