admin管理员组

文章数量:1204415

In his Eloquent Javascript, Haverbeke claims that (page 16):

"In a JavaScript system, most of this data is neatly separated into things called values. Every value has a type, which determines the kind of role it can play. There are six basic types of values: numbers, strings, Booleans, objects, functions, and undefined values."

But Crockford in Javascript: The Good Parts says:

"The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections. In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects."

Now, at least under V8 I get this:

> typeof function(){};
'function'
> typeof {};
'object'

I don't understand if object is a type and function is an object or if function and object are both types. I guess I'm missing the distinction between primitive types and other kind of types (composite types?).

In his Eloquent Javascript, Haverbeke claims that (page 16):

"In a JavaScript system, most of this data is neatly separated into things called values. Every value has a type, which determines the kind of role it can play. There are six basic types of values: numbers, strings, Booleans, objects, functions, and undefined values."

But Crockford in Javascript: The Good Parts says:

"The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections. In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects."

Now, at least under V8 I get this:

> typeof function(){};
'function'
> typeof {};
'object'

I don't understand if object is a type and function is an object or if function and object are both types. I guess I'm missing the distinction between primitive types and other kind of types (composite types?).

Share Improve this question asked Mar 14, 2013 at 2:08 user1700840user1700840 4512 silver badges12 bronze badges 3
  • Everything in JS is an Object. It is just like saying a square is a rectangle, but a rectangle is not a square. – Derek 朕會功夫 Commented Mar 14, 2013 at 2:11
  • 2 @Derek: Not quite everything. JS has primitives (non objects), and some of them have an object "wrapper", but not all. – the system Commented Mar 14, 2013 at 2:19
  • It' easiest to just think of a function as a special type of object, much like an array is a special type of object. They have the capabilities of an object, but also other capabilities that make them a different type of object. – jfriend00 Commented Mar 14, 2013 at 2:25
Add a comment  | 

2 Answers 2

Reset to default 24

They're a type of object.

The typeof is "function":

typeof (function() {}) === "function" // true

The internal [[Class]] is [object Function]:

({}).toString.call(function() {}) === "[object Function]" // true

They're an instance of the Function constructor prototype:

(function(){}) instanceof Function // true

They're an instance of the Object constructor prototype:

(function(){}) instanceof Object // true

You need to be careful when talking about types in javascript. Values have a Type, which may be one of the following:

  1. Undefined
  2. Null
  3. Boolean
  4. String
  5. Number
  6. Object

Perversely, the value returned by the typeof operator is not the Type, it's a string that is the same as the Type for most values, but is different for:

  1. Null returns 'object', even though its Type is Null
  2. An object that implements [[Call]] returns function, even though its Type is Object
  3. Host objects can return anything they like other than one of the restricted values

So the bottom line is that the Type of a function is Object, but typeof someFn returns function.

本文标签: Are functions objects or types in JavascriptStack Overflow