JS solution using function closures