|
| 1 | +// PART 0 |
| 2 | +function instructionGenerator() { |
| 3 | + function multiplyBy2(num) { // simple declaration just like any other data type |
| 4 | + return num * 2; |
| 5 | + } |
| 6 | + return multiplyBy2; |
| 7 | +} |
| 8 | + |
| 9 | +let generatedFunction = instructionGenerator(); |
| 10 | +console.log(generatedFunction(10)); |
| 11 | +console.log() |
| 12 | + |
| 13 | +// PART 1.1 |
| 14 | +function outer() { |
| 15 | + let counter = 0; |
| 16 | + function incrementCounter() { |
| 17 | + counter++; |
| 18 | + } |
| 19 | + incrementCounter(); |
| 20 | +} |
| 21 | + |
| 22 | +outer(); |
| 23 | + |
| 24 | +// incrementCounter(); // wont work |
| 25 | + |
| 26 | +// PART 1.2 since outhas been popped out then its done(look at the call stack) |
| 27 | +function funcGenerator() { |
| 28 | + let counter = 0; // NB: counter now becomes like private access specifier in java coz you can only have getters and setters inside of the function |
| 29 | + function incrementCounter() { |
| 30 | + counter++; |
| 31 | + console.log(counter); |
| 32 | + } |
| 33 | + return incrementCounter; |
| 34 | +} |
| 35 | + |
| 36 | +let myNewFunction = funcGenerator(); let func2 = myNewFunction; |
| 37 | +myNewFunction(); // oh oh you can't find counter in the local and then the global...huh we did not get an error |
| 38 | +// BUT BEFORE THAN YOU LOOK IN THE orange box/Bagack 1st before looking at global...Bagpack==LexicalScopeGenerator |
| 39 | +myNewFunction(); |
| 40 | +let anotherFunction = func2; //funcGenerator();// myNewFunction; // IMP funcGenerator makes a new func |
| 41 | +anotherFunction(); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +// PART MY |
| 46 | +function getterFuncGenerator() { |
| 47 | + let counter = 0; |
| 48 | + return function () { |
| 49 | + return counter; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function setterFuncGenerator() { |
| 54 | + let counter = 0; |
| 55 | + return function(newValue) { |
| 56 | + counter = newValue; |
| 57 | + console.log(counter) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +let func1 = setterFuncGenerator(); |
| 63 | +func1(10); |
| 64 | +console.log();console.log();console.log();console.log(); |
| 65 | + |
| 66 | +function Counter(initialValue=0) { |
| 67 | + let counter = initialValue; |
| 68 | + return { |
| 69 | + getCounter: function () { // getter COVE->ClosedOverVariableEnvironment |
| 70 | + return counter; |
| 71 | + }, |
| 72 | + setCounter: function (newValue) { // setter COVE->ClosedOverVariableEnvironment |
| 73 | + counter = newValue; |
| 74 | + }, |
| 75 | + incrementBy: function (amount) { // method // COVE->ClosedOverVariableEnvironment |
| 76 | + counter += amount; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +let countObj = Counter(); |
| 81 | +console.log(countObj.getCounter()); |
| 82 | +countObj.setCounter(10); |
| 83 | +console.log(countObj.getCounter()); |
| 84 | +countObj.incrementBy(20); |
| 85 | +console.log(countObj.getCounter()); |
| 86 | + |
| 87 | +console.log(); |
| 88 | + |
| 89 | +countObj = Counter(100); |
| 90 | +console.log(countObj.getCounter()); |
| 91 | +countObj.setCounter(10); |
| 92 | +console.log(countObj.getCounter()); |
| 93 | +countObj.incrementBy(20); |
| 94 | +console.log(countObj.getCounter()); |
0 commit comments