Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider revising example 2.49 #5

Closed
addyosmani opened this issue May 12, 2011 · 4 comments
Closed

Consider revising example 2.49 #5

addyosmani opened this issue May 12, 2011 · 4 comments
Labels

Comments

@addyosmani
Copy link
Member

Original ticket:

I'm not sure example 2.49 is a good example to explain what a closure is.
It has a loop, which might be unnecessary, and a click event, which depends upon the user doing something, rather than simply running the program.

By breaking it out into three (3) distinct functions, I was able to step into (F11) it.
I mention F11 vs. F10 because I've grown used to using F10 to step over jQuery itself.

 function Function1() {
 var text = 'Hello '; // local variable
 var Function2 = function() { // Just because this was defined, doesn't mean it was run.
 alert(text); // text was declared outside of Function2
 }
 text = 'Goodbye ';
 return Function2; // In fact it won't run until Function3 is actually called.
}
 var Function3 = Function1(); // This gets back 'Function2' as a return, but doesn't run Function2
 Function3(); // This actually runs Function2

This helped me to realize that closures are simply variables that are kept after the function is completed, the same as 'static' variables are in Visual Basic.

Notes:

Another ticket that mentions this particular example should be revised is: https://github.com/rmurphey/jqfundamentals/issues/56, however they point out that we perhaps shouldn't be using jQuery in the example given it's the JS fundamentals chapter.

Dan appears to have provided a revised version of the example that's library agnostic here https://github.com/rmurphey/jqfundamentals/issues/56, however we should decide whether we want to stick to using jQuery in parts of this chapter or switch to vanilla JavaScript.

Leaving open for discussion.

@gjohnson
Copy link
Contributor

gjohnson commented Jun 1, 2011

Looking through the other examples in this chapter, none of them use jQuery. I think the best way to handle it would be to write it in vanilla JS and mention how its good to grasp this concept because of how often you end up writing closures when using jQuery... or something. Similar to how jQuery is mentioned at the end of the Testing Type section.

I also think that the code examples could be rewritten to something more fundamental, I could be slow but creating closures during loop iterations and Function.bind is a pretty big leep for something labeled "101". :-)

@addyosmani
Copy link
Member Author

I agree that it may not make sense to be using jQuery in this chapter for consistency. Would you like to take a stab at integrating the library agnostic version of the code into this section (it's mentioned in the ticket)?. If not, I can do it :)

@gjohnson
Copy link
Contributor

gjohnson commented Jun 1, 2011

Yeah, I can handle that!

@ajpiano
Copy link
Member

ajpiano commented Nov 2, 2012

The current article on closures (https://github.com/jquery/learn.jquery.com/blob/master/page/javascript-101/closures.md) no longer contains this jQuery example. I think it's safe to close this given the vintage of this issue. If someone wants to work on the closures article more, there should be a separate issue for that.

@ajpiano ajpiano closed this as completed Nov 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 participants