Live data from Hacker News

What's a Closure?

nathansjslessons.appspot.com

11–20 of 60 posts

Re: What's a Closure?

#11
post #4

I found that structure of teaching awesome! If you already know some JavaScript you can skip the beginning and start right away at 8, "Nested Functions". Otherwise if you know any other programming language just start with the lessons and reach the lessons and learn about closures in less than 10 minutes, and -- maybe even more important -- learn many of JavaScript's basic on the way. I really hope the author of this…

See also http://ejohn.org/apps/learn/ and http://code.pageforest.com/ and http://www.stanford.edu/class/cs101/

And similar ideas for teaching java interactively in the browser: http://ijava.cs.umass.edu/ http://math.hws.edu/javanotes/

The idea itself is old, search for 'active essays' for example, and most recently wolfram alpha's computational document format.

Re: What's a Closure?

#13
This was a clever use of unit tests to help you "pass" each lesson. I've read a bunch of tutorials on closures, but this one really helped me "get" it. Sure, it takes liberties and glosses over things... but seeing it grow right before my eyes was really helpful.

Re: What's a Closure?

#14
is it me or this is confusing?

"Define a function named generate that takes one argument, a function f. It should return an array that consists of the function f and another function that doubles its input."

Re: What's a Closure?

#15
post #14

is it me or this is confusing? "Define a function named generate that takes one argument, a function f. It should return an array that consists of the function f and another function that doubles its input."

Define a function

  var ... = function( ... ){ ... };
named generate

  var generate = function( ... ){ ... };
that takes one argument, a function f

  var generate = function( f ){ ... };
It should return an array

  var generate = function( f ){ return [ ... ]; };
that consists of the function f

  var generate = function( f ){ return [ f, ... ]; };
and another function

  var generate = function( f ){ return [ f, function( ... ){ ... } ]; };
that doubles its input

  var generate = function( f ){ return [ f, function( x ){ return 2*x; } ]; };

Re: What's a Closure?

#16

Or there is this -- http://reprog.wordpress.com/2010/02/27/closures-finally-expl... -- which takes a much more informal approach. (Disclaimer: I wrote it myself, so it reflects my own rather practical moment of enlightenment rather than rigorous computer science.)

Nice. I also took a crack at explaining them - here's my version, in case it helps anyone.

http://sleeplessgeek.blogspot.com/2009/12/so-what-are-these-...

Re: What's a Closure?

#19
Wow, I love the flow!

Except JSLint. JSLint barfs all over otherwise valid JavaScript and provides really unhelpful error messages. For a minute, I thought that I forgot how to write valid JavaScript.... Hate! Hate! Hate! You're teaching folks to program, not write syntactically pure JS.

But drop that and the experience is great.

Re: What's a Closure?

#20
post #19

Wow, I love the flow! Except JSLint. JSLint barfs all over otherwise valid JavaScript and provides really unhelpful error messages. For a minute, I thought that I forgot how to write valid JavaScript.... Hate! Hate! Hate! You're teaching folks to program, not write syntactically pure JS. But drop that and the experience is great.

(Creator here) Ha, yeah JSLint really provokes strong emotions.

Before I added in JSLint, if you misplaced one semicolon or forgot a parentheses, none of the tests would run and it would just say "program failed". That was incredibly frustrating to me. I figured that if I was getting frustrated, and I was the creator of the tests, then random users would be REALLY frustrated. So I added in JSLint. Now you get line and column numbers for exactly where the syntax broke down.

I'm actually happy following all of JSLint's suggestions. But my first test user, my wife, had other thoughts. She filled out her first answer, saw the whitespace errors, and said "hells no". So I turned off the whitespace checking. She seemed OK with it after that.

Which checks in particular are annoying? I can change the default parameters of JSLint to be less strict about some things. But I don't want to give up syntax checking entirely.

Post reply on HN