The whole thing is wrapped as an anonymous function (you could also call it function run() { …}) because that avoids pollution of the (overpopulated) global namespace, and, above all, means that your own local variables appear grouped in a separate panel of the the Safari JavaScript debugger.
(Wrapping in an anonymous function to get a local namespace sometimes gets called ‘the module pattern’ in discussions of Javascript)
The name rebindings at the start are a fairly traditional way of ensuring default fall-back values for the arguments of the main function. Have to say that I’m moving away from that now – prefer to avoid mutation and use fresh names, lower down, that take values from the arguments if they are provided.
JavaScript functions are nestable objects like any other object (you can convert them to strings, use them as values in records, etc etc). Given the ‘module pattern’ of an outermost anonymous function, you will tend to see some function definitions at the top of the module. (The compiler lifts them there anyway if you place them further down).
One use of the module pattern in OS X scripting is that the arguments to the outer (anonymous) function can be an interface to some broader context. If you are running the JavaScript in a shell script, they might be bash variables bound to KM variable values.