0

Hi I'm beginner learning javascript. What is difference between following 2 constructors?

function Animal(name) {
        this.name = name;
        this.walk = function walk(destination) { //here function has name 'walk'
                console.log(this.name,'is walking to',destination);
        };
}

and

function Animal(name) {
        this.name = name;
        this.walk = function (destination) { // but no function name
                console.log(this.name,'is walking to',destination);
        };
}

Thank you in advance!

msjo
  • 1
  • 1
    Giving names to functions can help debugging. Otherwise, they behave pretty much the same. – John Dvorak Jul 15 '16 at 08:24
  • Also, if you were to invoke `walk` recursively, you could refer to the function as `walk` within itself (as opposed to having to keep a separate reference). – nils Jul 15 '16 at 08:25
  • The fact the function expression is inside a constructor function is irrelevant. – Quentin Jul 15 '16 at 08:26

1 Answers1

2

Named function vs anonymous function - not much a difference. When an error will be thrown, you'll get more accurate stack trace with named functions.

Oskar Szura
  • 2,469
  • 5
  • 32
  • 42