Guest User

Untitled

a guest
Oct 6th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. // follow along in the interpreter: node or browser developer console
  2.  
  3. // variable whose value is anonymous function
  4. var square = function(num) {
  5. return num * num;
  6. }
  7.  
  8. // named function
  9. function cube(num) {
  10. return square(num) * num
  11. }
  12.  
  13. // local vs global scope
  14. // from eloquent javascript
  15. var x = "outside";
  16.  
  17. var f1 = function() {
  18. var x = "inside";
  19. };
  20. f1();
  21. console.log(x);
  22.  
  23. var f2 = function() {
  24. x = "inside";
  25. };
  26. f2();
  27. console.log(x);
  28.  
  29. // functions within functions
  30. // from eloquent javascript
  31. function noisy(f) {
  32. return function(arg) {
  33. console.log("arg: ", arg);
  34. var val = f(arg);
  35. console.log("val: ", val);
  36. return val;
  37. };
  38. }
  39.  
  40. // better noisy with apply
  41. function betterNoisy(f) {
  42. return function() {
  43. console.log("args: ", arguments);
  44. var val = f.apply(null, arguments);
  45. console.log("val: ", val);
  46. return val;
  47. };
  48. }
  49.  
  50. // "optional" arguments
  51. // from eloquent javascript
  52. function power(base, exponent) {
  53. if (exponent == undefined)
  54. exponent = 2;
  55. var result = 1;
  56. for (var count = 0; count < exponent; count++)
  57. result *= base;
  58. return result;
  59. }
  60.  
  61. // simple closure
  62. // from eloquent javascript
  63. function myClosure(n) {
  64. var local = n;
  65. return function() { return local; };
  66. }
  67.  
  68. // password authentication using a closure
  69. // this is a closure because password is a local variable in the passwordChecker function
  70. // yet that variable is accessible in the anonymous inner function
  71.  
  72. // returns a function which checks if the guess is equal to password
  73. function passwordChecker(password) {
  74. return function(guess) {
  75. if (guess == password) {
  76. console.log('successfully authenticated');
  77. } else {
  78. console.log('failure.');
  79. }
  80. }
  81. }
  82.  
  83. var authenticator = passwordChecker('cool');
  84. authenticator('wrong'); // failure.
  85. authenticator('cool'); // successfully authenticated
  86.  
  87. //----------------------------objects in JS-------------------------
  88.  
  89. // constructor to use with new
  90. // when this function is called, an object is created
  91. // with a prototype that can be accessed with Teacher.prototype
  92. var Teacher = function(name, lecture) {
  93. this.name = name;
  94. this.lecture = lecture;
  95. }
  96.  
  97. // don't forget the new keyword
  98. var sherif = new Teacher('sherif', function() {
  99. console.log('what do you want to cover today? how about ajax?');
  100. });
  101.  
  102. // because the new keyword was used, julian is associated with the Teacher prototype
  103. // therefore, julian can use any methods and properties associated with Teacher.prototype
  104.  
  105. Teacher.prototype.grade = function() {
  106. console.log('you get an A');
  107. }
  108.  
  109. // one way to write a constructor
  110. // you don't get OOJS prototype functionality
  111. var Mentor = function(name, lecture) {
  112. mentor = {};
  113. mentor.name = name;
  114. mentor.lecture = lecture;
  115.  
  116. return mentor;
  117. }
  118.  
  119. var amadou = Mentor('amadou', function() { console.log('enthusiastic encouragement'); });
  120.  
  121. Mentor.prototype.help = function() { console.log('read the error'); } // should apply to all mentors??
  122.  
  123. amadou.lecture() // works
  124. amadou.help() // error
  125.  
  126. // new won't solve our problems here with the help function
  127. var christianne = new Mentor('christianne', function() { console.log('jQuery is great'); })
  128. christianne // { name: 'christianne', lecture: [Function] }
  129.  
  130. christianne.lecture() // works
  131. christianne.help() // still doesn't work TypeError: christianne.help is not a function
  132.  
  133. // phase 3 content, read ahead if you are interested
  134.  
  135. // module using an immediately invoking function expression
  136. // same password checking functionality as above
  137. var soundcloud; // this is the module
  138. (function(exports) {
  139. var API_KEY = 'secretkey';
  140.  
  141. exports.authenticate = function(key) {
  142. if (key === API_KEY) {
  143. console.log("authentication successful");
  144. }
  145. else {
  146. console.log("go away spammer");
  147. }
  148. }
  149. })(soundcloud = {});
  150.  
  151. soundcloud.authenticate('fake'); // go away spammer
  152. soundcloud.authenticate('secretkey'); // authentication successful
  153. soundcloud.API_KEY = 'evilkey'; // doesn't alter the API_KEY within the module. it's a new property that doesn't matter
  154. soundcloud.authenticate('evilkey'); // does not work
  155.  
  156. // augmentation i.e. adding new properties to an existing "module"
  157.  
  158. (function(exports) {
  159. exports.playMusic = function() {
  160. console.log("boomshakalaka");
  161. };
  162. return exports;
  163. }(soundcloud || {}));
  164.  
  165. soundcloud.playMusic();
  166.  
  167. // sub-modules i.e. module within a module
  168. soundcloud.rapModule = (function(exports) {
  169. exports = {};
  170. exports.catchPhrase = 'yo yo yo';
  171.  
  172. return exports;
  173. }());
  174.  
  175. soundcloud.rapModule.catchPhrase;
Add Comment
Please, Sign In to add comment