JavaScript design pattern (with source code)