Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Right here's my question how are the cool kids laying out their javascript these days? I've been looking around and from what I can tell everyone seems to be doing it different some people contain their functions inside object literals, some seem to just list there functions and have all there variable listed above. So what does eveyone think how should I be laying out my code.

var logsomestuff ="somestuff";

function logIt(){ 
   console.log(logsomestuff);
}

or maybe

logIt = {
   logsomestuff: "somestuff",
   logIt: function(){
      console.log(this.logsomestuff);  
   }
}

I don't know what do people think, best way to layout your js? above are just two example I'm sure there are a ton more. Thanks for anyhelp. I posted the same question of stack overflow but apparently it's better off here.

share|improve this question
What do you call "layout the code"? Your question doesn't make much sense. – Florian Margaine Aug 21 '12 at 21:36
That depends entirely on what you're doing. If you have an object, you list it methods inside it. If you have a function, you make it. I don't see the question here. – Zirak Aug 21 '12 at 21:38

2 Answers

(function() { // because it is cool
  var logsomestuff = "somestuff"; // notice two space indent. 
  function logIt() { // spaces before braces
    console.log( logsomestuff ); // spaces inside brackets.
  }
}());

Now really, the cool kids all wrap their stuff up in

$(function() {

});

or

$(document).ready(function() {

});

but I never knew why....

share|improve this answer
If you don't wrap the code in $(function(){}) or $(document).ready(function{}()) (which mean the same thing), then the dom might not be fully rendered by the time the javascript is executed. Which can cause issues – Matt Phillips Aug 21 '12 at 23:07
1  
But this fiddle doesn't work :( jsfiddle.net/Ralt/QGLrr – Florian Margaine Aug 21 '12 at 23:32
3  
LOL! priceless – rlemon Aug 21 '12 at 23:33
1  
@Matt: If there are DOM elements involved, it can cause issues. If there aren't, then it generally won't. But if you don't have jQuery loaded already, $(anything) would cause you problems of its own. – cHao Aug 21 '12 at 23:33
@cHao I agree with you. Sorry I didn't specify what "Which can cause issues" a little more in depth! – Matt Phillips Aug 22 '12 at 0:38
show 3 more comments

I guess what you mean is how others commonly "contain" their code. Well, with that comes namespacing which protects your variables and functions from global pollution and collision and a quick way to do that is to actually store variables and functions in an object literal:

var myNamespace = {
    property : 'value',
    aFunction : function(){
        console.log('things to do');
    }
}

myNamespace.property     //gives 'value'
myNamespace.aFunction(); //logs 'things to do'

Another common way to do it is to pass the global namespace into your private working space, by using a closure.

(function(){

    //do whatever you wish in here without code collision

    function innerbar(){
        console.log('from bar');
    }

    function innerbaz(){
        console.log('from bar');
    }

    //expose a namespace with bar on it referencing to innerbar
    //innerbaz is not exposed therefore it remains inside the closure
    window.myNamespace = {
        bar : innerbar
    }

}());
share|improve this answer
1  
@FlorianMargaine interesting link. – Joseph the Dreamer Aug 21 '12 at 23:45
Mostly saying that what you're doing is useless :p Also, passing document when you use jQuery isn't really useful, is it? – Florian Margaine Aug 21 '12 at 23:46
@FlorianMargaine haven't really given much thought about the code. just got used to using boilerplates which used those patterns. – Joseph the Dreamer Aug 21 '12 at 23:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.