News for hackers. Stuff that matters.
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @08:21AM (#863955)
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")

Re:JavaScript Programming Patterns by chunkyslink on Tuesday October 06, 2009 @08:30AM (#863972)
>> 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 about it.

Re:JavaScript Programming Patterns by extension on Tuesday October 06, 2009 @10:07AM (#864114)
That 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.
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @08:43AM (#864001)
My 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.

Re:JavaScript Programming Patterns by axod on Tuesday October 06, 2009 @09:06AM (#864022)
Leaving them out leads to horrible ambiguity and potential issues. Up to you though :)
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @09:27AM (#864049)
I 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.

Re:JavaScript Programming Patterns by yannis on Tuesday October 06, 2009 @09:22AM (#864039)
You 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.
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @09:25AM (#864043)
Not only is that more thrifty, but it is necessary. Thanks for catching the typo!
Re:JavaScript Programming Patterns by monos on Tuesday October 06, 2009 @09:26AM (#864046)
sorry, but why would carry the extra _obj around? why not simple do it like in my example above?
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @10:42AM (#864220)
That 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.

Re:JavaScript Programming Patterns by haydenchambers on Tuesday October 06, 2009 @08:28AM (#863967)
nice... except for the semicolons... i guess you're not minifying your code afterwards?
Re:JavaScript Programming Patterns by thegrossman on Tuesday October 06, 2009 @08:35AM (#863983)
I 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.

Re:JavaScript Programming Patterns by sprsquish on Tuesday October 06, 2009 @10:07AM (#864112)
I'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.
Re:JavaScript Programming Patterns by ejs on Tuesday October 06, 2009 @05:52PM (#865392)
I 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

Re:JavaScript Programming Patterns by sprsquish on Wednesday October 07, 2009 @09:48AM (#866683)
I'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