| Your approach is essentially the 'module pattern'. This article describes some other reasons you might not want to use it:
http://snook.ca/archives/javascript/no-love-for-module-patte... I lead a somewhat large (~10000 sloc) project which uses the module pattern everywhere. I think the best argument against the module pattern is the difficulty of debugging. I can no longer just pop open a console and inspect objects. Responses to your individual points: >> * you cannot use previous OOP experience While unfortunate I believe this is largely unavoidable. Javascript's object system is not 'classical' inheritance. The module pattern, however, is also not 'classical' inheritance. Regardless of OOP technique, the New Javascript Programmer must learn about the object system. >> * there are MANY ways to do OOP using prototypes There are also many forms of the module pattern. You just showed me another one I hadn't seen before. Then there's immediately invoking function expressions, which some people like to do like this (function () { var exports = {}; return exports; })()); and others like to do like this !function() { var private = 4; return { getPrivate: function () { return private; }; }(); >> * 'this' has different meanings depending on context Can't argue against this one. >> * prototypes make existing objects unreliable -> requires prior knowledge about libraries and APIs (leaky abstraction) I'm not sure I see the argument here. >> * building an object with prototypes requires far more mental steps: .. I'm not sure you really need to know about all of these things.. but sure, prototypal inheritance is confusing to those with a 'classical' background. As for the advantages: >> * conforms to established industry and business standards in OOP I don't see why prototypal inheritance does not. If you look at CoffeeScript, it even appears classical. >> * requires less knowledge about language details I have to disagree here. Explaining object literals and immediately invoking functions and closure isn't all that easy. >> * very transparent, no sugar code or libs required That's true. >> * neat code There's no reason why new/this/prototypal code can't be neat! >> * design patterns blah blah There's no reason why new/this/prototypal code can't do design patterns! >> * protection against corruption I've never seen this as an issue in practice, but my code isn't running in the browser. Unless you're talking about protection from other programmers, in which case, I would quote Guido van Rossum. >> * no this, that or self variables Could be a good thing or a bad thing. This means that you have to know what's public and what isn't, rather than it being explicit. I rather like it when things are explicit. Sometimes we sacrifice a little simplicity for a little flexibility or a little efficiency. Sometimes it's worth it, sometimes it isn't. |
So why am I not allowed to share something that works just because it may not fully comply with the 'preferred javascript style culture'?
What is this about?