Is there 2 ways to write functions?


#1




@.@ ???

I see 2 ways to write functions…
so can anyone tell me whats the major differences or practical differences?
why does one require a semicolon at the end of the function and the other one does not?


#2

Yep, there are two ways, but we should only be using the one way in our levels. This level still used an old method that we’ve since stopped using. (I fixed it :slight_smile: )

The difference is subtle, but briefly,

function myFunction() {

}

is the standard way of defining a function. It doesn’t need a semicolon because it’s not a statement, it’s a function declaration.

When you do:

var myFunction = function() { 

};

It’s assigning a function to the variable myFunction, which is more like a normal statement, it’s just harder to see since it’s on multiple lines:

var i = 0;
var myFunction = function() { };

Back when we used this instead of hero, we used the other way of defining functions, because it would make sure that the this keyword was referring correctly to the hero object. Now we have a special hero variable and don’t need to worry about this, which is a tricky topic in JavaScript.


#3

oh cool XD glad that I spotted some unwanted code.