2

I am learning JS and came across the concept of Javascript. I also learned about two different ways of creating function

//anonymous
    var fun1=function()
    {
    }

//named 
    function fun2()
    {
    }

I searched web and found out some important differences like:
1. We can call named function before its declaration while in case of anonymous its not possible.
2. fun1() be defined during runtime while allocation of the other one will be defined during parse-time

Now my question is why the prototype of fun1() is pointing to Object while prototype of fun2 is pointing to itself? I tried a lot to understand this but not getting it properly.

enter image description here

Also what type of function should we use at what scenario?

Ankur Aggarwal
  • 2,803
  • 3
  • 27
  • 47
  • possible duplicate of [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Bergi Mar 16 '14 at 10:24
  • @Bergi Hi I checked that link already. I want to know about the prototype differences – Ankur Aggarwal Mar 16 '14 at 10:26
  • Sorry that was too fast. Maybe you should mention "prototype" in the question title as well - and also that you have "seen that in console" ;-) – Bergi Mar 16 '14 at 10:29

1 Answers1

3

Now my question is why the prototype of fun1() is pointing to Object

It's not. It is pointing to an object. Expand it and you will find that it's a normal prototype object, not the Object constructor.

prototype of fun2 is pointing to itself?

It's not. It is pointing to a normal prototype object as well. The only difference is that fun2 is a named function, so your console will display everything that has a .constructor == fun2 as "a fun2 object".

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Thanks. I checked that and it was same as you told. Can you also please tell me in which scenarios we should go to these options – Ankur Aggarwal Mar 16 '14 at 14:04
  • Named functions are easier to debug. In certain situations, you need to use a function expression, and those shouldn't be named if you need IE 8 compatibility. The linked "duplicate" is more detailed on that question. – Bergi Mar 16 '14 at 14:47