function onMouseClickFunction() {
$mapCanvas.click(function (e) {
var x = cursor.getCursorPositionInCanvasX(e.pageX),
y = cursor.getCursorPositionInCanvasY(e.pageY);
what is the variable scope in this example is it function onMouseclickFunction or perhaps the anonymous function of jquery?
What i ment is that javascript uses hoisting creating variables at the top of its parent function, so where is hoisting done in this example?
Assuming you mean the variables
xandy, then it is the anonymous function passed toclick().varalways scopes to the current function.Hoisting means that when you use
var, the variable will be scoped for the function, even if you have previously tried to use it.i.e.
If the inner function was called, it would alert
undefinedbecausevar x = 2creates a newxin the scope of the inner function (varis hoisted), but the assignment doesn’t take place until after the alert (since the assignment is not hoisted).All the
varstatements in the question appear at the top of the function they appear in, so hoisting makes no difference.