28. Hello? Yes, This is Dog - different answer?


#1

So this is the correct answer:

// add the sayHello method to the Dog class 
// so all dogs now can say hello
Dog.prototype.sayHello = function() {
    console.log("Hello this is a " + this.breed + " dog");
};

But my initial answer was this:

// add the sayHello method to the Dog class 
// so all dogs now can say hello
Dog.prototype = sayHello() {
    console.log("Hello this is a " + this.breed + " dog");
};

What is the difference between the two? Is it that you have to specify what is being added to the prototype by adding .methodName or .propName to the class Dog?

Not just for this case, but overall, I get confused between var varName = function() { and function varName() {.


#2

@yn1106,
With

var varName = function() {};

you create a variable varName to which you assign a so-called anonymous function
and you end the statement with a semi-colon-;
As the function is anonymous it is more difficult to keep track when =debugging=.

========================================================

With

function funcName() {}

you define a so-called named function,
note the definition is NOT ended by a semi-colon-;

=====================================================================================

Here a good article....
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.