4 ms·
I used to live by the "revealing module pattern", although I didn't know that's what it was actually called. Currently, I do something along these lines: var M
by thegrossman 17y ago
I used to live by the "revealing module pattern", although I didn't know that's what it was actually called. Currently, I do something along these lines:
var MyObject = function() {
var _obj = {}
var privateVariable = 'foo'
var privateMethod = function(){}
_obj.publicVariable = 'bar'
_obj.publicMethod = function(){}
return _obj
}()
To me, it makes the separation of public and private methods and variables easier to observe.
Also, an unrelated note: I've stopped using semicolons round about 6 months ago and have never looked back. It makes my life simpler, I'm happier, and my sex life has never been better.
EDIT: Removed syntax error (the "var" in front of "_obj.whatever")
- yannis 17y agoYou can even get more thrifty by saving on vars as well :) and conserve some more electrons besides those affected by semicolons. _obj.publicVariable = 'bar' _obj.publicMethod = function(){} Personally I cannot type any statement without an ending semicolon, it is a mannerism that stayed with me since I had to learn Pascal. It makes it difficult for me to do anything useful in Python and I am actually contemplating to see a therapist for this.
- thegrossman 17y agoNot only is that more thrifty, but it is necessary. Thanks for catching the typo!
- monos 17y agosorry, but why would carry the extra _obj around? why not simple do it like in my example above?
- thegrossman 17y agoThat works, too! The reason I like using _obj is because "this" is notoriously hard to work with in JS. If, inside foo(), you have an iterator that you pass a function to, "this" takes on a different meaning inside that function. Some people are really good at working out what "this" refers to, but I'm not. So I assign the main object to _obj just to be safe.
- haydenchambers 17y agonice... except for the semicolons... i guess you're not minifying your code afterwards?
- thegrossman 17y agoI tend to use minified libraries (JQuery, effects libraries, etc), but unminified application-specific code. Been burned too many times having to root out some obscure error on the live server, and not being able to do so because of minification. However, I've never had a problem in the cases where I have indeed minified unsemicoloned code. Has anyone else, or is the problem just theoretical? If others have encountered an actual problem, I might have to rethink this strategy for larger codebases.
- sprsquish 17y agoI've been burned several times by missing semicolons when minifying my code. Now I make sure they're always there. I'm also running everything through JSLint.
- ejs 17y agoI think JSLint can be a big help, especially with semicolons so I made a quick jQuery plugin to run things through it on page load. So I know exactly when something may be wrong. http://github.com/ejschmitt/AutoLint http://github.com/ejschmitt/AutoLint
- sprsquish 17y agoI'm using the Javascript Tools bundle for TextMate. It runs JSLint every time I save a javascript file. http://github.com/subtleGradient/javascript-tools.tmbundle http://github.com/subtleGradient/javascript-tools.tmbundle
- chunkyslink 17y ago>> I've stopped using semicolons round about 6 months ago and have never looked back. It makes my life simpler, I'm happier, and my sex life has never been better. This sounds pretty dangerous to me. Crockford has this to say http://bit.ly/r9EZO http://bit.ly/r9EZO about it.
- thegrossman 17y agoMy mom always told me that I had a limited supply of semicolons in my semicolon bucket, and to not waste them on languages where they aren't strictly required. Now, maybe she was just joshing me... but I'm not taking that chance.
- axod 17y agoLeaving them out leads to horrible ambiguity and potential issues. Up to you though :)
- thegrossman 17y agoI haven't had a single issue in the 6 months of daily Javascriptin' since I've stopped using them. As I mentioned in another thread, I don't minify my application code so maybe it's not as important as it is for other people.
- extension 17y agoThat example isn't very reasonable. Obviously, one would expect semicolons to be inserted after lines that form complete statements. It's not hard to ensure that intermediate lines don't do that. It's done for other languages that don't use semicolons.