This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
over 9 years

Question about Function Declarations (3/6) and (4/6)

In exercise 3/6, we create a function called printPerson() that looks roughly like this:

var printPerson = function(person) {
     console.log(person.firstName + " " + person.lastName);
}

After that exercise is done and we move on to the next function, printPerson() is then changed to this:

function printPerson(person) {
     console.log(person.firstName + " " + person.lastName);
}

Does anyone know why the declaration is changed? I kinda-sorta tried to figure it out by myself by calling the printPerson() function using the new declaration, but a (I think) Reference error was thrown.

Any input would be appreciated, but please try not to go waaay over my head :-\ .

Answer 53d48ef0631fe9039b0012aa

1 vote

Permalink

Unless we had the author to ask this question of, an answer as to why is, well, out of the question. The two forms are functionally the same under correct conditions.

The first condition would be the existence of defined values for the indentifierNames, person.firstName and person.lastName.

The second and perhaps the more important, the function expression must be defined before any reference to it. Consider the following:

doSum(17,25);
function doSum(a,b){
    return a+b;
}
// 42

but this,

doProduct(6,7);
var doProduct = function(u,v){
    return u * v;
}
// TypeError: undefined is not a function

A term to study while exploring functions is hoisting. Here I leave you to continue your research…

points
Submitted by Roy
over 9 years

1 comments

Thanks, I’ll look up and hopefully make more sense of it.