Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't understand. WHY do you want to use one main function or class? Why not break things up into modules, classes, and functions similar to the way you do in C#?

Look at the source code of any well-written JavaScript library or app and you will find quite a bit of modularity. Some examples, from small to large:

http://osteele.com/sources/javascript/functional/functional....

http://github.com/jdalton/fusejs/tree/master/src/

http://github.com/aptana/activejs/tree/master/src/active_rec...

It makes sense to use one global object as a namespace, but within this namespace you can have as many classes, objects, and functions as you like. Look at the source of jQuery, for example: http://code.jquery.com/jquery-1.4.1.js

Even if you have a single class, you can define that classes' members across many different files. You can concatenate all the files together at build- or deployment-time (for performance reasons) but keep them separate in your source tree. For example, the jQuery source code I linked above is actually a concatenation of half a dozen different source files. Just use the "module pattern" to wrap each file in an anonymous function, and have it export its public interfaces to objects/classes in your single global namespace: http://www.yuiblog.com/blog/2007/06/12/module-pattern/



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: