Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A closure does not mean that it necessarily is returned by a function.
- // A closure generally is a function that has access to the declaration context's scope.
- // A closure example without returning a function
- var oddNumber = 3;
- function isOddNumberIsReallyOdd() {
- return oddNumber % 2 === 1;
- }
- console.log(isOddNumberIsReallyOdd(oddNumber));
- // Output: true
- // A closure example with returning a function
- function getWordRepeater(n) {
- // return a word that repeated "n" times
- return function(word) {
- let result = "";
- for (let i = 0; i < n; i++) {
- result += word;
- }
- return result;
- };
- }
- var wordRepeater = getWordRepeater(3);
- console.log(wordRepeater("hi"));
- // Output: hihihi
- // A high order function does mean that it returns a function
- function getOddDetector() {
- return n => n % 2 === 1;
- }
- var oddDetector = getOddDetector();
- console.log(oddDetector(4));
- // Output: false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement