English subtitles

← 06-30 Anonymous Functions Solution

Get Embed Code
3 Languages

Showing Revision 1 created 06/10/2012 by Amara Bot.

  1. The first part of our return value is just the word function.
  2. This is to separate it from a number like 3 or 4
  3. and to allow us to tell if the user mistakenly tries to call something
  4. that's not a function later.
  5. We then list the parameters which we got right from the abstract syntax tree,
  6. the body of the function, which we also got right from the abstract syntax tree,
  7. and the environment in which it was defined--this one, env.
  8. And we're passing in env here instead of some global environment or whatnot
  9. and this is what's going to allow local functions to see local variables.
  10. This is why things like makegreeter work.
  11. They can refer back to variables that were currently in scope
  12. when they were defined.
  13. Many of you may notice a striking similarity between this and our previous code
  14. for handling function declarations.
  15. In fact, our previous code just had 1 more step
  16. where we added this value to the environment.
  17. For an anonymous function, we don't add it to the environment
  18. unless the user assigns it.
  19. But this code should look really, really familiar.
  20. We have something almost exactly like it for handling functions at the top level
  21. as JavaScript elements.