jQuery jAM, Season 6

Exercise 8.2

The Locked Box

	 
function withBoxUnlocked(body){
	var locked = box.locked;
	if(!locked)
	return body();
	
	box.unlock();
	try{
		return body();
	}finally{
		box.lock();
	}
}

withBoxUnlocked(function(){
	box.content.push("gold piece");
});

try{
	withBoxUnlocked(function(){
		throw new Error("Pirates on the horizon! Abort!");
	});
}catch (e){
	console.log("Error raised:", e);
}

console.log(box.locked);
	 
	

To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.