Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. 1. What is meant by the following statement?
  2.  
  3. # A function doesn't have to return in order to be called a closure. Simply accessing variables outside of the immediate lexical scope creates a closure.
  4.  
  5. Answer: closure can return variablees so they're available in parent scope
  6.  
  7. var sayHello = function(name) {
  8. var text = 'Hello, ' + name;
  9. return function() {
  10. console.log(text);
  11. };
  12. };
  13.  
  14. var helloTodd = sayHello('Todd');
  15. helloTodd(); // will call the closure and log 'Hello, Todd'
  16.  
  17. sayHello('Bob')(); // call the returned function without assignment
  18.  
  19. 2. How does the following code work?
  20.  
  21. function findLargestNumber(arrayOfNumbers) {
  22. return Math.max.apply(null, arrayofNumbers);
  23. };
  24.  
  25. Math.max returns the largest of numbers
  26. .apply pass an array as parameters to function
  27.  
  28. 3. What is the purpose of the module pattern?
  29.  
  30. JS coding pattern (design pattern). Easy to read, maintain and edit. Use Objects in a nice way without repeating this and prototype.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement