Try / Finally trickery
Just a simple experiment to see what can be done with try / finally
HTML
<h3>Output:</h3>
<ul id="output"></ul>
CSS
#output {
list-style: disc;
padding-left: 20px;
}
JavaScript
// Simple convenience output function
function output() {
var container = document.getElementById('output'),
item = document.createElement('li'),
msgs = [],
i, l;
console.log(arguments);
for (i = 0, l = arguments.length; i < l; ++i) {
msgs.push(arguments[i].toString());
}
item.innerHTML = msgs.join(' ');
container.appendChild(item);
}
// A starting point which does some output, calls a function, and does more output
function start() {
output('start: Calling subroutine');
subroutine();
output('start: Past subroutine call');
}
// The real experiment: Can we get code in the subroutine to run after the return?
function subroutine() {
output('subroutine: Starting');
try {
output('subroutine: Code in a try');
output('subroutine: More code in a try, about to return');
return;
output('subroutine: In try, past return');
}
finally {
output('subroutine: Code in finally');
return "foo";
}
output('subroutine: Code after try/finally block');
}
document.addEventListener('DOMContentLoaded', function() {
// I like a DOM ready notice
output('DOM Ready!');
// Start our experiment
start();
}, false);