Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /*FUNCTIONS:
  2. Functions are blocks of code that hold 'instructions' telling the
  3. computer what to do and how to get it done.
  4.  
  5. They go through two phases:
  6. 1. they are declared with the keyword function followed by a set of parentheses
  7. and curly braces.
  8. -The instructions for what to do go inside the curly braces.
  9. 2. they are then called/invoked/executed by placing
  10. it outside of the function
  11.  
  12. Sometimes functions take parameters/arguments; these are values or
  13. 'information' that the function needs in order to get the job done.
  14. Parameters are basically named variables while arguements are the actual
  15. values used.
  16. */
  17. //Example:
  18. /*This function passes two numbers as arguments, adds them together
  19. and then returns the sum.
  20.  
  21. function add(numOne, numTwo) {
  22. var sum = numOne + numTwo;
  23. return sum;
  24. }
  25. console.log(add(1, 2)); //prints the number 3
  26. */
  27. /*
  28. There are different types of functions including named functions,
  29. anonymous functions and function expressions
  30.  
  31. NAMED FUNCTIONS:
  32. Are just functions declared with a name. They have names
  33. so that they can be called again later on in the program.
  34. */
  35. //Example:
  36. /*This function is called subtractTwo; it takes a number
  37. subtracts 2 from it and returns the answer.
  38. */
  39. 'use strict';
  40.  
  41. function subtractTwo(num) {
  42. var product = num - 2;
  43. return product;
  44. }console.log(subtractTwo(7)); //prints 5
  45.  
  46. /*ANONYMOUS FUNCTIONS:
  47. Are declared without a name; they are typically used for
  48. passing through other functions
  49. */
  50. //Example:
  51. function printNumString(num, action) {
  52. console.log(action(num));
  53. }printNumString(2, function (num) {
  54. return num.toString();
  55. });
  56. //prints the number 2 as a string
  57.  
  58. /*FUNCTION EXPRESSIONS:
  59. When an anonymous function is assigned to a variable
  60. or constant.
  61.  
  62. **YOU CAN ONLY CALL A FUNCTION EXPRESSION AFTER IT HAS BEEN
  63. ASSIGNED NOT BEFORE**
  64. */
  65. //Example:
  66. var add = function add(numOne, numTwo) {
  67. return numOne + numTwo;
  68. };
  69. var sum = add(3, 3);
  70. console.log(sum); //prints 6
  71.  
  72. /*SCOPE:
  73. When we talk about scope we're talking about whether
  74. we are inside or outside of a function and what information
  75. we have access to depending on where we are.
  76.  
  77. 1.There is the global scope(outside of a funtion)
  78. 2.There is the local/block scope (inside of a function)
  79.  
  80. children have access to the parent's information but
  81. the parent does NOT have access to the child's
  82. */
  83. //Example:
  84. var a = 'hi';
  85. function printString() {
  86. a = 'hi there';
  87. }console.log(a);
  88. /*does NOT throw an error because child has access to
  89. variable a in parent scope and can change value*/
  90.  
  91. function printString() {
  92. var a = 'hi';
  93. console.log(a);
  94. }
  95. console.log(a);
  96. /*throws an error because a is declared
  97. within the function; it is local scoped*/
  98.  
  99. /*CLOSURE:
  100. Functions can carry references to variables in its
  101. parent scope inside of its definition body.
  102.  
  103. The function forms a closure around the environment
  104. in which it was defined. That means function called
  105. has access to variables of its parent scope.
  106. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement