I have a simple function in jQuery and I wanted to test out the scope.
$(function() {
function showTest() {
alert('test');
}
showTest(); // within scope so its OK
});
$(function() {
showTest(); // out of scope
}
showTest(); // out of scope
How can I reference the showTest() that is within the first jQuery statement? I’m trying to access the showTest using stringByEvaluatingJavaScriptFromString but I cannot seem to hit it. I’ve tried both the out of scope methods and can’t think of another way to reference the method inside the jquery.
Isolating variables and functions from outside code is what anonymous functions are supposed to do. If you want to run
showTestoutside of your anonymous function, you have to remove the anonymous function: