

Tell HN: Will answer your javascript questions for the next few hours - geuis

Need help with a javascript problem? I'm open for the next few hours to answer questions or help with your code.<p>Leave a comment here, hit me on twitter @geuis, or email geuis.teses@gmail.com
======
pacomerh
What are good ways to organize files to avoid having a big "chorizo" all the
way down.

~~~
geuis
I like the way the jQuery build system works. Basically you order your js
files from the base up. So functionality that is used more commonly by other
files is lower in the stack (i.e. loaded first). If you organize your code so
that dependencies are contained, this also has the benefit that if your app
doesn't need particular sections of code, you can remove the ones highest in
the chain without adverse side effects.

~~~
pacomerh
Having the files contained from bottom up sounds smart do you have any links
you can share that show this technique

~~~
geuis
Take a look at this. <http://www.youtube.com/watch?v=i_qE1iAmjFg>

This is Paul Irish giving a breakdown of interesting bits about jQuery. He
talks about this technique roughly 50 minutes in.

------
darkxanthos
What's the best way to handle variable scoping when using closures?

How about when calling object methods from DOM events?

~~~
geuis
var x = (function(){

var inner_foo = 1; //internal to closure

return function(){

    
    
    		var foo = 'foo';
    		var bar = foo + inner_foo;
    		
    		//this function has access to variables from the outer scope.
    		//inner_foo is still accessible
    		
    		console.log( inner_foo, foo, bar ); //1, 'foo', 'foo1'
    		
    	}
    	

})();

//Edit a couple minutes later: I'm realizing I may not be understanding
exactly what you're asking for in the 2nd question. If you elaborate a bit...

Since an event will execute either an assigned or anonymous function, the same
rules towards variable scope in your closures still applies when the events
execute. Having a function executed by a click event isn't fundamentally
different than executing a function directly.

~~~
tantalor
It is when it's a function of an object.

