Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are functions objects or types in Javascript?

Tags:

javascript

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?).

like image 943
user1700840 Avatar asked Mar 14 '13 02:03

user1700840


People also ask

Is function a type in JavaScript?

In JavaScript functions are first-class objects - a function is a regular object of type function .

Is function same as object?

Object is a function, but not every object is a function.

What are functions in JavaScript?

A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

Is a function a datatype?

function is a StreamBase data type and a reserved keyword. Use a constructor for the function data type to create a custom expression language function whose components are built-in functions, math operators, and even other functions.


2 Answers

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
like image 117
the system Avatar answered Oct 07 '22 10:10

the system


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.

like image 32
RobG Avatar answered Oct 07 '22 12:10

RobG