3 ms·
Tell HN: Will answer your javascript questions for the next few hours
Need help with a javascript problem? I'm open for the next few hours to answer questions or help with your code.
Leave a comment here, hit me on twitter @geuis, or email geuis.teses@gmail.com
- darkxanthos 16y agoWhat's the best way to handle variable scoping when using closures? How about when calling object methods from DOM events?
- tantalor 16y agohttps://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en/JavaScript/Reference/Global...
- geuis 16y agovar 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 16y agoIt is when it's a function of an object.
- pacomerh 16y agoWhat are good ways to organize files to avoid having a big "chorizo" all the way down.
- geuis 16y agoI 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 16y agoHaving the files contained from bottom up sounds smart do you have any links you can share that show this technique
- geuis 16y agoTake a look at this. http://www.youtube.com/watch?v=i_qE1iAmjFg 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.