Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. What is meant by the following statement?
- # 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.
- Answer: closure can return variablees so they're available in parent scope
- var sayHello = function(name) {
- var text = 'Hello, ' + name;
- return function() {
- console.log(text);
- };
- };
- var helloTodd = sayHello('Todd');
- helloTodd(); // will call the closure and log 'Hello, Todd'
- sayHello('Bob')(); // call the returned function without assignment
- 2. How does the following code work?
- function findLargestNumber(arrayOfNumbers) {
- return Math.max.apply(null, arrayofNumbers);
- };
- Math.max returns the largest of numbers
- .apply pass an array as parameters to function
- 3. What is the purpose of the module pattern?
- 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