0

I got confused about these two , var a = {} and var a = function(){}
a is an object in the first case , a is a function in the second case. And function is also a object. what is the difference?

Craig Suchanec
10.9k4 gold badges34 silver badges39 bronze badges
asked Mar 23, 2015 at 23:33
4
  • 3
    Try to invoke an object and you'll see (or to send to server a function)... Commented Mar 23, 2015 at 23:36
  • question is very vague. typeof for each is different. Please provide more context as to why you are asking the difference Commented Mar 23, 2015 at 23:36
  • Each has a different constructor: jsfiddle.net/qm52hc1u/1 Commented Mar 23, 2015 at 23:37
  • 1
    duplicate stackoverflow.com/questions/4125479/… Commented Mar 23, 2015 at 23:44

1 Answer 1

1

A function is a type of an object in Javascript, but a empty function (function(){}) is very different from an empty object ({}). The easiest way to see the difference is to just execute them and see what they do differently. Using the console you can play around and see how they are different.

 var emptyFunction = function() {}
 console.log(emptyFunction) // function(){}
 console.log(typeof emptyFunction) //"function"
 var emptyObject = {}
 console.log(emptyObject) //Object{}
 console.log(emptyObject) // "object"
 emptyFunction() // returns undefined because your function has no return
 emptyObject() // Uncaught TypeError: object is not a function

The first thing is the type of them are different things as the typeof operator indicates. It shows you what Javascript thinks of the type The last line is where you really start to see the difference. A function is able to be invoked by using () to call it. An object does not have that ability and it will cause an error because the type Object doesn't have a behavior defined for it that involves using the ()

As you pointed out though, a function is just a specific type of object so it can do the same sort of things an object. So we can do something like this:

emptyFunction.foo = function(){ return 'foo';} console.log(emptyFunction.foo()) //'foo' emptyObject.bar = function(){ return 'bar';} console.log(emptyObject.bar()) //'bar'

As you can see when it comes down to it, the difference is a function is a specialized object that can be invoked. You can read more about functions in Javascript here

answered Mar 23, 2015 at 23:51
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for response. Since they are similar. In which situation, var a = {} is better choice and in which situation , var a = function () {} is a better choice?
Asking that questions shows a lack of understanding of functions and objects in general. You really need to look at the fundamentals of the language. An empty function is completely useless as it just returns undefined and doesn't do anything. You wouldn't use it in any real scenario I can think of. You use a function when you need a sub-routine in your program. A subroutine that returns undefined and does nothings is useless.
Sorry I did not make it clear. I meant var a = { code goes here} and var a = function (){ code goes here} . They are not empty. These two things can do the same thing. So, in which situation , one is desirable?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.