English subtitles

← 03-38 Revenge Of Javascript

dummy description

Get Embed Code
3 Languages

Showing Revision 1 created 05/04/2012 by Amara Bot.

  1. Now that we know how to specify grammars for well-balanced expressions
  2. and arithmetic and well-balanced webpage tags in HTML,
  3. we're going to return our attention to JavaScript.
  4. JavaScript is actually very similar to Python.
  5. Just like I showed you a formal grammar for HTML,
  6. we're going to work our way up to seeing a formal grammar for JavaScript.
  7. But before we get there, I just want to make sure
  8. that we really understand how JavaScript programs are interpreted.
  9. I'm going to show you a few more in Python and in JavaScript for comparison.
  10. Over here on the left, I have a Python function
  11. that computes the absolute value of its integer argument.
  12. If you give me a negative number, like -5, I will return positive 5.
  13. If you give me a positive number, like +9 million, I will return 9 million.
  14. The return value of this function is always either a zero a positive number.
  15. Now I'm going to write the same thing in JavaScript
  16. to provide for a comparison.
  17. Everything I've drawn in blue is a special keyword or punctuation mark used by the language.
  18. For example, to define a function in Python we use "def."
  19. In JavaScript, we write out the word function, but then it's still our choice what to call it--
  20. I'm called it "absval" in both cases--
  21. and how many parameters it should receive and what they're names are.
  22. In both cases, we have one parameter named x.
  23. In Python we use colons and tabbing to tell what the body of a function is,
  24. what the then branch for an if is, what the else branch for an if is.
  25. In JavaScript, we use curly braces and closing curly braces
  26. to denote this sort of lexical or syntactic scope.
  27. This is sort of curly brace 1, and it matches up with closed curly brace 1 over here,
  28. 2 matches up with 2, and 3 matches up with 3.
  29. But in general the logical structure, the flow or the meaning, is the same.
  30. In both cases we check to see if x is less than 0, and we return 0 minus x in that case,
  31. or just x alone in the other case.
  32. One of the most important operations in any language is printing out information,
  33. displaying it to the screen so that we can see the result of computation
  34. or just to help us debug.
  35. In Python we use the print procedure.
  36. We pass it a bunch of strings.
  37. Here I'm adding together the strings "hello" and exclamation mart
  38. to make a very enthusiastic greet--"hello!"
  39. Over here on the right, I'm showing the same thing in JavaScript.
  40. The equivalent of "print" is "document.write" or perhaps just "write."
  41. In this class, we'll almost always abbreviate it down to just "write" to save space.
  42. If you're familiar with object-oriented programming, which is not required for this class,
  43. you might guess what the dot is about.
  44. We might talk more about that later.
  45. One of the key differences, though, is that all of our JavaScript functions have to have
  46. these open and close parentheses like a mathematical function
  47. has parentheses around its argument.