Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="functions">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /* 1. Functions:
  13. * Functions are programs within programs or, a type of data encapsulating reusable
  14. * code. To start with, we must declare or define a function. A function takes
  15. * a parameter and runs it through a block of code. But how does it do that?
  16. * This leads to the second part of using functions. Once we have declared our
  17. * function, we need to invoke it. When we invoke or call our function, we pass an
  18. * argument through our parameter slot. Let's look at a function.
  19. */
  20.  
  21. function nameHorse(name) { //Declares the function, nameHorse, and defines our parameter.
  22. console.log("My horse's name is " + name); //This is what the function will do.
  23. }
  24.  
  25. nameHorse("pickle"); //Our invocation where we pass in our argument.
  26.  
  27. nameHorse("taco"); /*This prints taco. The above prints pickle, and the only thing that
  28. * changed is our argument.
  29. */
  30.  
  31. /* The parameter is simply a placeholder for input, whereas the argument is the
  32. * actual input. Our parameter, name, takes in the argument or input, of whatever we want.
  33. */
  34.  
  35. // Named functions are functions that have been assigned to a variable or constant.
  36. multiply(9, 14); //This has been added to demonstrate hoisting to scope. Prints 126.
  37.  
  38. function multiply(number1, number2) {
  39. console.log(number1 * number2);
  40. }
  41.  
  42. multiply(12, 44);
  43. /* This invokes our function and logs 528, the result of our arguments
  44. * passing through our function. It's important to note that named functions are hoisted
  45. * to the top of their scope. This means that I can invoke this function earlier in
  46. * our program. Now let's try assigning a function to a variable.
  47. */
  48.  
  49. var tuxedo = function(color){return "Slick " + color;};
  50. var purchase = tuxedo("blue");
  51.  
  52. console.log(purchase); // This logs "Slick blue".
  53.  
  54. /* In the above, we created a variable and assigned a function to it. We then invoked
  55. * the function and assigned the invocation to yet another variable. We also gave an
  56. * input, and returned a value, which functions can do optionally.
  57. */
  58.  
  59.  
  60. //Let's look at copy by value vs. copy by reference.
  61.  
  62. var gravity = 45;
  63.  
  64. var flying = gravity;
  65.  
  66. console.log(flying); //logs 45.
  67.  
  68. flying = 99;
  69.  
  70. console.log(gravity);//logs 45.
  71. console.log(flying); //logs 99.
  72.  
  73. // Here we have copy by value, which links to the value in the container.
  74.  
  75.  
  76.  
  77. var baseBall = {bat: "wood"};
  78.  
  79. var game = baseBall;
  80.  
  81. console.log(game.bat);//logs "wood".
  82.  
  83. game.bat = "aluminium";
  84.  
  85. console.log(baseBall.bat); //"logs "aluminium".
  86.  
  87. /*Here we have copy by reference where the containers are both linked to the same object
  88. * and editing the object from one container, changes the values for both.
  89. */
  90.  
  91.  
  92. /*Scopes! Scopes, local and global, determine where in a program a variable or value
  93. * can be used. Local is enclosed with a function, and cannot be accessed from outside of
  94. * that function. Global can be accessed anywhere in the program after it is declared.
  95. */
  96.  
  97. function talkDirtyToMe() {
  98. var nasty = 3;
  99. console.log(nasty);
  100. }
  101. talkDirtyToMe();//prints 3
  102.  
  103. //console.log(nasty); //will not print, returns error. commented out.
  104. console.log(chicken);//returns undefined.
  105. var chicken = "cluck";
  106. console.log(chicken);//returns "cluck".
  107.  
  108.  
  109. /* Finally, closures are a fancy pants way of saying a function inside a function
  110. * can access values in the parent scope.
  111. */
  112.  
  113.  
  114. function cooker(food) {
  115. var topping = "cheese";
  116. return function(string) {
  117. return (string + topping + food);
  118. }
  119. }
  120.  
  121. console.log(cooker("Hello"), ("duck"));//Prints the return and "duck".
  122. </script>
  123.  
  124.  
  125.  
  126. <script id="jsbin-source-javascript" type="text/javascript">/* 1. Functions:
  127. * Functions are programs within programs or, a type of data encapsulating reusable
  128. * code. To start with, we must declare or define a function. A function takes
  129. * a parameter and runs it through a block of code. But how does it do that?
  130. * This leads to the second part of using functions. Once we have declared our
  131. * function, we need to invoke it. When we invoke or call our function, we pass an
  132. * argument through our parameter slot. Let's look at a function.
  133. */
  134.  
  135. function nameHorse(name) { //Declares the function, nameHorse, and defines our parameter.
  136. console.log("My horse's name is " + name); //This is what the function will do.
  137. }
  138.  
  139. nameHorse("pickle"); //Our invocation where we pass in our argument.
  140.  
  141. nameHorse("taco"); /*This prints taco. The above prints pickle, and the only thing that
  142. * changed is our argument.
  143. */
  144.  
  145. /* The parameter is simply a placeholder for input, whereas the argument is the
  146. * actual input. Our parameter, name, takes in the argument or input, of whatever we want.
  147. */
  148.  
  149. // Named functions are functions that have been assigned to a variable or constant.
  150. multiply(9, 14); //This has been added to demonstrate hoisting to scope. Prints 126.
  151.  
  152. function multiply(number1, number2) {
  153. console.log(number1 * number2);
  154. }
  155.  
  156. multiply(12, 44);
  157. /* This invokes our function and logs 528, the result of our arguments
  158. * passing through our function. It's important to note that named functions are hoisted
  159. * to the top of their scope. This means that I can invoke this function earlier in
  160. * our program. Now let's try assigning a function to a variable.
  161. */
  162.  
  163. var tuxedo = function(color){return "Slick " + color;};
  164. var purchase = tuxedo("blue");
  165.  
  166. console.log(purchase); // This logs "Slick blue".
  167.  
  168. /* In the above, we created a variable and assigned a function to it. We then invoked
  169. * the function and assigned the invocation to yet another variable. We also gave an
  170. * input, and returned a value, which functions can do optionally.
  171. */
  172.  
  173.  
  174. //Let's look at copy by value vs. copy by reference.
  175.  
  176. var gravity = 45;
  177.  
  178. var flying = gravity;
  179.  
  180. console.log(flying); //logs 45.
  181.  
  182. flying = 99;
  183.  
  184. console.log(gravity);//logs 45.
  185. console.log(flying); //logs 99.
  186.  
  187. // Here we have copy by value, which links to the value in the container.
  188.  
  189.  
  190.  
  191. var baseBall = {bat: "wood"};
  192.  
  193. var game = baseBall;
  194.  
  195. console.log(game.bat);//logs "wood".
  196.  
  197. game.bat = "aluminium";
  198.  
  199. console.log(baseBall.bat); //"logs "aluminium".
  200.  
  201. /*Here we have copy by reference where the containers are both linked to the same object
  202. * and editing the object from one container, changes the values for both.
  203. */
  204.  
  205.  
  206. /*Scopes! Scopes, local and global, determine where in a program a variable or value
  207. * can be used. Local is enclosed with a function, and cannot be accessed from outside of
  208. * that function. Global can be accessed anywhere in the program after it is declared.
  209. */
  210.  
  211. function talkDirtyToMe() {
  212. var nasty = 3;
  213. console.log(nasty);
  214. }
  215. talkDirtyToMe();//prints 3
  216.  
  217. //console.log(nasty); //will not print, returns error. commented out.
  218. console.log(chicken);//returns undefined.
  219. var chicken = "cluck";
  220. console.log(chicken);//returns "cluck".
  221.  
  222.  
  223. /* Finally, closures are a fancy pants way of saying a function inside a function
  224. * can access values in the parent scope.
  225. */
  226.  
  227.  
  228. function cooker(food) {
  229. var topping = "cheese";
  230. return function(string) {
  231. return (string + topping + food);
  232. }
  233. }
  234.  
  235. console.log(cooker("Hello"), ("duck"));//Prints the return and "duck".
  236.  
  237.  
  238. </script></body>
  239. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement