Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // Functions:
  12.  
  13. // A function is a procedure- a set of statements that perform a task or
  14. // calculate a value.
  15.  
  16. // First, we must decalre the function within the scope we plan to
  17. // call it. The proper way to set up a function is:
  18.  
  19. // 1. Use the function keyword and name your function.
  20. // 2. List your parameters to the function in parentheses and seperated
  21. // by commas.
  22. // 3. The statements that define the function will be inside curly brackets {}.
  23.  
  24. // The proper syntax for a function is:
  25.  
  26. function double(number){
  27. return (number + number);
  28.  
  29. }
  30. double(5); // returns 10
  31. //When you call or invoke this function, you will pass an arguemet to the parameter that you want
  32. //to use for number. This function double will return that number + the same number,
  33. //doubling the number.
  34.  
  35. //The parameters will be the places where the arguements will be passed when
  36. // the function is invoked. These will not change in the function and are used
  37. //like variables. The argeuments can be any value and can change, as long
  38. //as it works with the statements inside of the function.
  39.  
  40. // You can also assign a function to a variable by declaring the variable and
  41. // assigning it to the function.
  42. // ex.
  43.  
  44. var add = function(number) {
  45. return number + number;
  46. }
  47. add(2);
  48. //returns 4
  49.  
  50. //You can also call the function down the line anywhere in your code. Below,
  51. //the function 'add' was called and added to the above function 'double. This
  52. //comes in handy so you can join results. Functions are convenient because you
  53. //can pass multiple values to the function and get various results without
  54. //having to retype the function.
  55.  
  56. console.log(add(5) + double(10));
  57. //returns 30
  58. console.log(add(5) + 3);
  59. //returns 13
  60. console.log("I want to buy " + double(10) + " apples!");
  61. //returns: "I want to buy 20 apples!"
  62.  
  63. //There are 5 primitive values that can be passed in to a function by
  64. //the means above. These are 'undefined', 'null', 'boolean', 'string' and 'number'.
  65. //Non-primitive values will be passed by reference. Passing by reference
  66. //means that the underlying objects have the same memory location, they are referring
  67. // to or referencing the same value. Inside the function, you can change the value and it
  68. // will change the value inside the memory location and print a new value.
  69. //ex.
  70.  
  71. var myObject = {score1:100, score2:47};
  72. console.log(myObject);
  73. //returns
  74. // [object Object] {
  75. // score1: 100,
  76. // score2: 47
  77. // }
  78.  
  79. function changingValues(obj) {
  80. obj.score2 = 93;
  81. console.log(obj);
  82. }
  83. changingValues(myObject);
  84. // returns the object with the new value inside.//
  85. // [object Object] {
  86. // score1: 100,
  87. // score2: 93
  88. // }
  89.  
  90.  
  91. // Now,if you console 'myObject' again, like we did
  92. // before changing the value. It will return with the new value
  93. // assigned from inside the function. This is because the value
  94. // was changed by memory location. It has been permanently changed.
  95.  
  96. console.log(myObject);
  97. //returns//
  98. // [object Object] {
  99. // score1: 100,
  100. // score2: 93 <--New Value
  101. // }
  102.  
  103.  
  104. // Function Scopes:
  105.  
  106. // Scopes are essentially boundaries that you are working within
  107. // inside of your code. There are local scopes and global scopes.
  108. // When working inside of a function, anything between the curly brackets
  109. // of your function is it's local scope. Anything outside of those brackets
  110. // (and not inside another local scope) is in the global scope.
  111. // Any variable declared inside of a functions local scope cannot
  112. // be accessed from outside the local scope. HOWEVER, a function can access
  113. // any variables that are inside of its parent scope. So, if a function is
  114. // declared globally, it can access all of the variables that are avaiable globally,
  115. // but if it is declared inside of another function, it can only access it's own
  116. // variables AND the variables of the function it's declared inside of.
  117. //ex.
  118.  
  119. // The following variables are defined in the global scope
  120. var myScore = 100,
  121. myScore1 = 57,
  122. name = 'Lindsey';
  123.  
  124. // This function is defined in the global scope (not inside another function)
  125. function plus() {
  126. return myScore + myScore1;
  127. }
  128.  
  129. plus(); // Returns 157
  130.  
  131. // A local scope example
  132. function getScore() {
  133. var scoresA = 71,
  134. scoresB = 20;
  135.  
  136. function sum() {
  137. return name + ' scored ' + (scoresA + scoresB);
  138. }
  139.  
  140. return sum();
  141. }
  142.  
  143. console.log(getScore()); // Returns "Lindsey scored 91"
  144.  
  145. //Above, the function sum can access the name because the variable 'name'
  146. //is delcared globally and therefore can be accessed. Conversely
  147. // the function plus() cannot use the variables scoresA, scoresB
  148. // because they are inside of a local scope of function getScore().
  149. //ex.
  150.  
  151. function plus() {
  152. return myScore + myScore1 /*+ scoresA*/; //<-- cannot access this
  153. }
  154.  
  155. console.log(plus());
  156. //returns:
  157. //"ReferenceError: scoresA is not defined
  158.  
  159. //Closures:
  160. /*Closures are very important in JavaScript. As stated above, functions
  161. can be nested inside of other functions. This gives the nested function
  162. access to all of the variables and functions inside of the outter function.
  163. However, the outter function does not have access to the variables and functions
  164. defined inside the inner function. These boundaries are a security of sorts for
  165. the variables of the inner function. A CLOSURE is created when the inner
  166. function is somehow made available to any scope outside the outer function.
  167. */
  168. //ex.
  169. var dog = function(name) { // The outer function defines a variable called "name"
  170. var getName = function() {
  171. return name; // The inner function has access to the "name" variable of the outer function
  172. }
  173. return getName; // Return the inner function, thereby exposing it to outer scopes
  174. }
  175. myDog = dog('Sandy');
  176.  
  177. console.log(myDog()); // Returns "Sandy"
  178.  
  179. //For this example you are referring to variables insde the function
  180. //in a chain type of reaction.
  181.  
  182. // Calling myDog accesss dog('Sandy'). 'Sandy' is the arguement
  183. // passed into the dog function which accesses the getName function.
  184. // This function returns the dogs name. // "Sandy"
  185. </script>
  186.  
  187.  
  188.  
  189. <script id="jsbin-source-javascript" type="text/javascript">// Functions:
  190.  
  191. // A function is a procedure- a set of statements that perform a task or
  192. // calculate a value.
  193.  
  194. // First, we must decalre the function within the scope we plan to
  195. // call it. The proper way to set up a function is:
  196.  
  197. // 1. Use the function keyword and name your function.
  198. // 2. List your parameters to the function in parentheses and seperated
  199. // by commas.
  200. // 3. The statements that define the function will be inside curly brackets {}.
  201.  
  202. // The proper syntax for a function is:
  203.  
  204. function double(number){
  205. return (number + number);
  206.  
  207. }
  208. double(5); // returns 10
  209. //When you call or invoke this function, you will pass an arguemet to the parameter that you want
  210. //to use for number. This function double will return that number + the same number,
  211. //doubling the number.
  212.  
  213. //The parameters will be the places where the arguements will be passed when
  214. // the function is invoked. These will not change in the function and are used
  215. //like variables. The argeuments can be any value and can change, as long
  216. //as it works with the statements inside of the function.
  217.  
  218. // You can also assign a function to a variable by declaring the variable and
  219. // assigning it to the function.
  220. // ex.
  221.  
  222. var add = function(number) {
  223. return number + number;
  224. }
  225. add(2);
  226. //returns 4
  227.  
  228. //You can also call the function down the line anywhere in your code. Below,
  229. //the function 'add' was called and added to the above function 'double. This
  230. //comes in handy so you can join results. Functions are convenient because you
  231. //can pass multiple values to the function and get various results without
  232. //having to retype the function.
  233.  
  234. console.log(add(5) + double(10));
  235. //returns 30
  236. console.log(add(5) + 3);
  237. //returns 13
  238. console.log("I want to buy " + double(10) + " apples!");
  239. //returns: "I want to buy 20 apples!"
  240.  
  241. //There are 5 primitive values that can be passed in to a function by
  242. //the means above. These are 'undefined', 'null', 'boolean', 'string' and 'number'.
  243. //Non-primitive values will be passed by reference. Passing by reference
  244. //means that the underlying objects have the same memory location, they are referring
  245. // to or referencing the same value. Inside the function, you can change the value and it
  246. // will change the value inside the memory location and print a new value.
  247. //ex.
  248.  
  249. var myObject = {score1:100, score2:47};
  250. console.log(myObject);
  251. //returns
  252. // [object Object] {
  253. // score1: 100,
  254. // score2: 47
  255. // }
  256.  
  257. function changingValues(obj) {
  258. obj.score2 = 93;
  259. console.log(obj);
  260. }
  261. changingValues(myObject);
  262. // returns the object with the new value inside.//
  263. // [object Object] {
  264. // score1: 100,
  265. // score2: 93
  266. // }
  267.  
  268.  
  269. // Now,if you console 'myObject' again, like we did
  270. // before changing the value. It will return with the new value
  271. // assigned from inside the function. This is because the value
  272. // was changed by memory location. It has been permanently changed.
  273.  
  274. console.log(myObject);
  275. //returns//
  276. // [object Object] {
  277. // score1: 100,
  278. // score2: 93 <--New Value
  279. // }
  280.  
  281.  
  282. // Function Scopes:
  283.  
  284. // Scopes are essentially boundaries that you are working within
  285. // inside of your code. There are local scopes and global scopes.
  286. // When working inside of a function, anything between the curly brackets
  287. // of your function is it's local scope. Anything outside of those brackets
  288. // (and not inside another local scope) is in the global scope.
  289. // Any variable declared inside of a functions local scope cannot
  290. // be accessed from outside the local scope. HOWEVER, a function can access
  291. // any variables that are inside of its parent scope. So, if a function is
  292. // declared globally, it can access all of the variables that are avaiable globally,
  293. // but if it is declared inside of another function, it can only access it's own
  294. // variables AND the variables of the function it's declared inside of.
  295. //ex.
  296.  
  297. // The following variables are defined in the global scope
  298. var myScore = 100,
  299. myScore1 = 57,
  300. name = 'Lindsey';
  301.  
  302. // This function is defined in the global scope (not inside another function)
  303. function plus() {
  304. return myScore + myScore1;
  305. }
  306.  
  307. plus(); // Returns 157
  308.  
  309. // A local scope example
  310. function getScore() {
  311. var scoresA = 71,
  312. scoresB = 20;
  313.  
  314. function sum() {
  315. return name + ' scored ' + (scoresA + scoresB);
  316. }
  317.  
  318. return sum();
  319. }
  320.  
  321. console.log(getScore()); // Returns "Lindsey scored 91"
  322.  
  323. //Above, the function sum can access the name because the variable 'name'
  324. //is delcared globally and therefore can be accessed. Conversely
  325. // the function plus() cannot use the variables scoresA, scoresB
  326. // because they are inside of a local scope of function getScore().
  327. //ex.
  328.  
  329. function plus() {
  330. return myScore + myScore1 /*+ scoresA*/; //<-- cannot access this
  331. }
  332.  
  333. console.log(plus());
  334. //returns:
  335. //"ReferenceError: scoresA is not defined
  336.  
  337. //Closures:
  338. /*Closures are very important in JavaScript. As stated above, functions
  339. can be nested inside of other functions. This gives the nested function
  340. access to all of the variables and functions inside of the outter function.
  341. However, the outter function does not have access to the variables and functions
  342. defined inside the inner function. These boundaries are a security of sorts for
  343. the variables of the inner function. A CLOSURE is created when the inner
  344. function is somehow made available to any scope outside the outer function.
  345. */
  346. //ex.
  347. var dog = function(name) { // The outer function defines a variable called "name"
  348. var getName = function() {
  349. return name; // The inner function has access to the "name" variable of the outer function
  350. }
  351. return getName; // Return the inner function, thereby exposing it to outer scopes
  352. }
  353. myDog = dog('Sandy');
  354.  
  355. console.log(myDog()); // Returns "Sandy"
  356.  
  357. //For this example you are referring to variables insde the function
  358. //in a chain type of reaction.
  359.  
  360. // Calling myDog accesss dog('Sandy'). 'Sandy' is the arguement
  361. // passed into the dog function which accesses the getName function.
  362. // This function returns the dogs name. // "Sandy"
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372. </script></body>
  373. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement