English subtitles

← 06-29 Anonymous Functions

Get Embed Code
3 Languages

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

  1. One of the cool features of our JavaScript interpreter
  2. is that it supports anonymous or nested functions.
  3. However, these features can be very hard to test, so let's try it out together.
  4. You'll recall this Python example from before
  5. where we had a nested function definition of greeter that we returned,
  6. and the final output of this program would be "hello" "gracie".
  7. I'm going to write this same program in JavaScript.
  8. Aside from some minor syntactic differences, the content is the same.
  9. We make a variable, greeting, initialize to "hola". Variable greeting is "hola".
  10. We're going to define this makegreeter function of 1 argument called greeting,
  11. define the makegreeter function of 1 argument called greeting.
  12. Then we're going to make this sort of nested function greeter
  13. that takes an argument, person, and here the local variable greeter is a function
  14. that takes an argument, person.
  15. So where in Python we used another def, in JavaScript we're using this function keyword.
  16. Argument person is the same.
  17. Instead of print we call write or document.write and we return the greeter.
  18. Sayhello is a variable of makegreeter("hello"),
  19. variable sayhello is the result of calling makegreeter on "hello"
  20. and then we do it at the end.
  21. So the real exciting part is here.
  22. In JavaScript you can use the word function to make a new function anywhere
  23. without really giving it a name, although we assigned it to the variable greeter
  24. almost immediately.
  25. So you can use it at the top level to make a function with a name,
  26. or you can use it lower down.
  27. See, here we just have function and then we're listing the arguments.
  28. We didn't put a name in here.
  29. This is sometimes called an anonymous function
  30. because it doesn't immediately have a name.
  31. So as a quiz, let's add support for those anonymous functions
  32. to our JavaScript interpreter.
  33. I'll write the first part, you fill in the key details.
  34. Anonymous functions are expressions.
  35. We know that since they can come on the right-hand side of something like this.
  36. Anything on the right-hand side of a var or an assignment statement is an expression.
  37. So there are a bunch of different types of expressions--
  38. numbers, strings, binary operators.
  39. Let's just handle the function part for now.
  40. So as a running example, let's say we have a function of 2 variables, x and y,
  41. that's going to return their sum.
  42. The abstract syntax tree we get for that will have function
  43. in this sort of identifier position telling us what this sort of node is,
  44. then it will have a list of the parameters,
  45. and then it will have the body list of statements.
  46. And what I'd like you to do for this quiz is fill in these 4 blanks.
  47. We want to return a particular value--
  48. value corresponding to a function.
  49. This is going to require you to think back to how we treated function definitions
  50. and function calls earlier in our interpreter.
  51. But here's a hint: A function was a for tuple containing the word function at the beginning
  52. and then also listing in some order the body, the environment, and the parameters.
  53. Fill it in.