50

I was reading this link JavaScript_syntax

This seems to be cyclic - that every function is an Object and every Object itself is a function. Which is the atomic one? Can someone explain in a better way?

gekrish
  • 2,201
  • 11
  • 29
  • 46

13 Answers13

64
  1. Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript. That means function inherits from object.

  2. Object instances can contain more instances which can be functions. That's what we call a "method" (since it has an automatic this variable).

  3. Since you can't "call" every Object instance, not every object is a function.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 9
    Functions are instances of `Function` (which probably *does* inherit from `Object`), as is mentioned in the OP's linked article: http://en.wikipedia.org/wiki/JavaScript_syntax#Function – FrustratedWithFormsDesigner Aug 10 '10 at 13:55
  • 7
    @gurupriyan.e: Try this: `var obj={}; obj();` That's "trying to call object" (i.e. using it like it was a function). Doesn't work. Hence an arbitrary object is not a function. – Aaron Digulla Aug 10 '10 at 14:40
  • var num = 5; In this line, num is an object as it has properties and a prototype defined. Is this correct or I'm missing something? Same should be true for string, and boolean ? Although, it seems like undefined and null are themselves different type altogether ? – Sid Jun 24 '17 at 08:05
  • 1
    Question regarding #1, How come `typeof null` is an `object`? – allenhwkim Sep 02 '17 at 16:25
  • @allenhwkim It's a bug in `typeof`. `null` really is a primitive like `5`. See https://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined, specifically this answer: https://stackoverflow.com/a/7968470/34088 – Aaron Digulla Sep 04 '17 at 09:17
20

I think this concept is often misunderstood.

A utility to visualize JS types relationship http://jstype.herokuapp.com/#/home

Javascript Data Types

  1. Primitive types - numbers, strings, booleans, null and undefined.
  2. All non-primitive types are object:

var foo = { }; 
var foo = [1, 2, 3]; 
var foo = function abc() { return "hello world"; }; 
var foo = new Number(30); 
var foo = new String("Hello World"); 
var foo = new Boolean(true); 
var foo = new RegExp(/[foo]+/);

// All 'foo` are object. 
  1. All primitive types have a corresponding Constructor Function wiz. Array, Number, String, Boolean, RegExp. As all functions are objects, they are objects too. So we can call them Constructor Function Objects.

  2. Most of the non-primitive type has prototype property where all inherited stuff lives. Math doesn't have prototype.

  3. All objects inherit from Object.prototype which inherits from null.
    object <- Object.prototype <- null

  4. All native functions inherit from Function.prototype which inherits from Object.prototype.
    function <- Function.prototype <- Object.prototype <- null

  5. Arrays inherit from Array.prototype which inherits from Object.prototype.
    array <- Array.prototype <- Object.prototype <- null

Must read MDN: Inheritance and prototype chain
To get confused Stackoverflow: prototype in JavaScript
Stack Overflow: Function prototype explained

Community
  • 1
  • 1
Rohit
  • 6,365
  • 14
  • 59
  • 90
  • 1
    `var foo = function abc() { return "hello world"; }; ` `typeof foo` is `function` here and not `object` – Rahul Yadav Mar 28 '18 at 09:22
  • @Rahul Yes, however `instanceof Object` returns true. The `typeof` operator, contrary to expectation, does not always give the "root type" -- it has a special case where if `Function.prototype` is within the prototype chain, it returns `function`, instead of the root type `object`. At base, all functions still inherit from `Object.prototype`. (This can be confirmed with `(function(){}) instanceof Object`, or `(function(){}).__proto__.__proto__ == Object.prototype`, both of which return true.) – Venryx Oct 26 '19 at 08:47
  • Does `null` inherits from something? – carloswm85 Nov 25 '21 at 22:41
8

Every function is an object. Objects can contain functions (methods) but an object is not necessary a function.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
8

Also Function is always a property of an object.

This mean that all functions in JavaScript is always bound to an object. If you don't specify an object to bind a function to it's bound to the window object (Also called global functions)

..fredrik

fredrik
  • 17,537
  • 9
  • 51
  • 71
3

It would be better to say that in JavaScript everything can be treated as an object, that includes primitive types as well as functions types; what the JavaScript interpreter does is that it automatically promotes your primitives and functions to their object wrapper types when you interact with them.

There is also a Function object, an a number of equivalent Wrappers for the other primitives in JavaScript, that means that you can even call methods on functions instances, like:

myFunction(someArg).call(this)

That being said, not every object is in fact a function.

Jose Diaz
  • 5,353
  • 1
  • 31
  • 29
  • but ... every `Object` is `function Object() { [native code] }` – Ekim Aug 12 '11 at 23:38
  • so every `Object` IS a function! – Ekim Aug 13 '11 at 13:31
  • unless `Object` is redefined like `Object=null` – Ekim Aug 13 '11 at 13:36
  • but in that case, null is a primitive and then not an object - not a valid example of an Object not being a Function. – RickB Mar 09 '15 at 16:39
  • Please, distinguish between an `Object` constructor/definition and an actual `object` at language level. It's a pain to see every answer and comment say the opposite of the previous one. `function Object` is how the base object is defined, but under the hood it is still an object with properties (`length`, `name`, etc.). – Kamafeather Sep 15 '20 at 15:06
3

As others have said, functions are objects that can be passed around by reference like other javascript objects. Not all objects are functions, only those that are declared as such.

You will often see methods declared like so:

var myFunc = function(foo, bar) {
    ...
};

This is to reinforce the fact that the method is a function object and as such it is a property of the object where it is defined, just like any other variable you might define with var.

This is the foundation of the most important feature in javascript, closure.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
jasongetsdown
  • 1,295
  • 1
  • 17
  • 22
  • But every Javascript object is also an instance of `Object`, and `Object` itself is a function (can be initialized with the `new` keyword, `new Object()`), and that function is an instance of `Function`, which is an instance of `Object` again ? – doubleOrt Sep 09 '17 at 11:11
1

Every function is an Object.

I'm not an javascript expert, but I cannot see how every Object is a function. (I can see how every object could be a function, but that's different)

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • 3
    `Object` IS `function Object() { [native code] }` (a function) and everything is an object ... ergo everything is a function ... hmmm.... – Ekim Aug 12 '11 at 23:37
1

Just for supplementary to Aaron Digulla's answer. In javascript not every object is a function. But Object,Array,String and many other built-in objects are functions that used with new operator to create object. I think this is what confused most people.

AllenMa
  • 19
  • 3
  • So, isn't every object an instance of `Object` ? Could you elaborate ? Is there a way to create objects that does not involve `Object` ? – doubleOrt Sep 09 '17 at 11:14
1

javascript everything is a hashtable. Ryan Dahl said this in one of his talks. thats what json is also; a hashtable definition. thats why u can access an object using array notation or object property notation. the value of the hashtable can be a function as well which is a hashtable. or rather an associative array type Object in the console u get { [native code] } which is a hashtable

godseyeview
  • 163
  • 2
  • 11
1

Object is an abstract data given to a class and that class is assigned to an object. Object can have properties and properties can hold values and functions. Or simply for the sake of making it easy to understand you can say that anything that is not primitive data type (number,string, boolean, unll & undefined) can be classified as an object.

Dan
  • 1,077
  • 7
  • 6
1

Quoting from Working with Objects - MDN Docs
section Using Object Initializers last paragraph:

"In JavaScript 1.1 and earlier, you cannot use object initializers. You can create objects only using their constructor functions or using a function supplied by some other object for that purpose. See Using a Constructor Function."

meant that all objects WERE functions! Specifically, upon evaluation, instances or instantiations of functions.

Literally, ALL objects of that vintage were created syntactically with constructs like:

 "newobj = new constructFunctor(arg1,arg2){this.prop1=arg1 /* etc */}(val1,val2)"

AND in the string that makes the object "newobj" there is the word "constructFunctor", the name of a function. The statement is intentionally quoted to impress the fact that it must be eval()'d to be executed. Prior to execution "newobj" is "equated to" a function because the statement MUST have one and "is" one by virtue of "constructFunctor"'s literal existence to define newobj's value upon execution. The quoting, and not, is very intentional in order to elucidate this abstraction. However, because JavaScript DOES have an eval function, this abstraction is in fact intentionally incorporated into the JavaScript language.

The legacy of this is still fundamental to JavaScript, though some syntactic short cuts have been added as "object initializers" using the shorthand notation like: "no={}". That the paragraph quoted above is still resident in the current documentation IS significant for the very reasons noted.

Furthermore, JavaScript also exemplifies the fundamental paradigms of Functional Programming. This defines everything as a function using the abstractions of Recursive Function Theory and the Lambda Calculus! For instance 0(), 1(), 2(), ... are the constant nonary functions better known as 0,1,2,3, ...

JavaScript is completely consistent with a Functional Programming Style approach rather than the common OOPS (pun intended Object Oriented Programming Style).

Ekim
  • 949
  • 1
  • 8
  • 9
-1

the selected answer by Aaron Digulla's is not 100% correct because it says,

Anything that is not a primitive type (undefined, null, number, string, boolean) is an object.

but a string is an object. That is why you can do things like:

myString="Hello World";

x = myString.length;
newString = myString.toUpperCase();
link = myString.link("http://www.hello-world.com/");

And many other methods can be applied to the string object.

You could also initialize the string like:

myString = new String("Hello, World!");

But because a string is also a datatype it is much easier to initialize it by just applying a value.

Not necessarily an answer to the question ... just a clarification/correction of Aaron Digulla's answer.

cre84.me
  • 7
  • 1
  • 6
    This is not technically correct. Strings *ARE* primitives in JavaScript. The fact that can you manipulate them with methods hints at what happens internally. Whenever you manipulate a string primitive JavaScript creates an object that inherits from the String object, performs an operation, then returns a string primitive. This happens on the fly. String primitives are actually immutable themselves in JS. – AdamSchuld Feb 12 '15 at 23:59
-2

The selected answer is wrong. In JavaScript everything is a function, except primitive types. Object itself is a function which is called function Object(). Use for example the following code:

<script>
    alert(Object);
</script>
Cosmin
  • 30
  • 1
  • 7
    `Object` is a function, but not every object is a function. – Andrew Magee Nov 21 '13 at 03:50
  • @AndrewMagee could you elaborate ? Aren't all Javascript objects descendants of that `Object` function ? – doubleOrt Sep 09 '17 at 11:28
  • 1
    @doubleOrt, I might be a little late, but this is true because `Object` is a constructor. It is its own variable and it is a function. All other objects are instances of this constructor and do not have anything to do with the type of the constructor itself. You can check this by opening the console and entering `({}).constructor === Object` – Gust van de Wal Jan 28 '20 at 15:20