When we are defining objects and functions in javascript we don't see much difference.So is their any thing to differ object from function ?
-
Check this out - http://stackoverflow.com/questions/17525450/object-vs-class-vs-function – AGrammerPro Jun 29 '16 at 17:11
-
1You can't serialize a function and you can't execute an object. – Patrick Roberts Jun 29 '16 at 17:11
-
2An object and a function are used for different purposes. If you see no difference then you don't understand programming in general. – Ram Jun 29 '16 at 17:17
-
JavaScript has first-class functions, so functions are instances of the Function class, which inherits from Object. Unlike Lisp, the only similarity between functions and data structures is the curly braces. – gcampbell Jun 29 '16 at 17:24
2 Answers
A good rule of thumb in JavaScript is that every object you see has, in one way or another, derived from the same Object. Functions are object in JavaScript, as seen here. So that's why they seem similar.
Edit: Primitive data types are not objects however. I believe this is the only exception.

- 805
- 1
- 11
- 27
For starters, almost everything in Javascript is an Object: Array, Function, Object literals, even Strings and Numbers can be temporary promoted to an object via the '.' operator, though they are not actually objects.
Every Javascript Object has built in properties (references), for example the proto reference which leads to to it's unique function constructor.
But functions are very unique objects, for starters they possess a code property that you invoke by a simple operator:
function a(){ console.log("hello") }
a();
They have a scope object reference, which contains all of it's fathers variables, allowing closure to be possible:
function a(){
var c = 1;
return function b(){
console.log("i have access to my father's vars" + c);
}
}
var c = a();
they have the ability to create execution contexts which you may read about in here:
http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/
And generally have many unique built-in features other objects don't have. Finally you may differ functions from object literals by the typeof operator:
typeof function(){} //function
typeof {} //object

- 611
- 1
- 9
- 21