albseb511 / fsd

Topics to cover for Full Stack
MIT License
168 stars 83 forks source link

closures notes #1

Open albseb511 opened 4 years ago

albseb511 commented 4 years ago
satya-hash commented 1 year ago

@albseb511


   <!-- function parent(){ -->
       let name = 'satya';
       function child(){
           console.log(name);
       }
       return child;
   }

   let newFunc = parent();
   newFunc(); # output: satya

   <!-- function makeCounter(){ -->
       let count = 0;
       function increment(){
           count++;
           console.log(count);
       }
       return increment;
   }

   let counter = makeCounter();
   counter(); #output: 1
   counter(); #output: 2
   counter(); #output: 3

   <!-- function person(name){ -->
       let age = 0;
       function getAge(){
           return this. age;
       }function setAge(newAge){
           this.age = newAge;
       }
       function getName(){
           return name;
       }
       return {
           getName,setAge,getAge
       };
   }

  let person1 = person("satya");
   console.log(person1.getName()); #output: satya
   person1.setAge(20);
   console.log(person1.getAge()); #output: 20

This is my first time answering GitHub issues. And I would like to take feedback.