Advantages
- Modern browsers are way faster to process JS.
- Trend – clients are doing what servers used to do
- Server is used only to generate data like json
- Client is used only to generate/render html
- Speed/efficient (no redundant data transfer between server client)
Disadvantage
- No compile time error checking
- Once a project becomes big, it is hard to upgrade
libraries/framework
without breaking something (at runtime) - Not properly organized (global methods, big script file)
Solution
- Enclose functions in namespace
- Modularize code (this way big file can be split)
- “use strict” (reason)
- Write unit test (to solve compile time checking)
here is an example -
(function (nsr, $, undefined) {
"use strict"
nsr.Module1 = nsr.Module1 || {};
nsr.Module1.variable1 = 'test';
nsr.Module1.function1 = function(){}
} (window.nsr = window.nsr || {}, jQuery));