GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — javascript-oop/encapsulation/closure.js at master · FaztWeb/javascript-oop Expand file tree/ Copy pathclosure.jsMore file actions 42 lines (31 loc) · 995 Bytes / Copy pathclosure.jsFile metadata and controls42 lines (31 loc) · 995 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 // convention-base approach allows to access the property anyway // so to fix this approach is not using properties for internal members // but variables in the constructor function TheatreSeats() { const seats = []; this.placePerson = function (person) { seats.push(person); }; } // Scope and closure // const greeting = "Hello world"; // function greets(person) { // const fullName = person.name + " " + person.surname; // function displayGreeting() { // console.log(greeting + " " + fullName); // } // displayGreeting(); // } // greets({ name: "Ryan", surname: "Ray" }); // In javascript we can acces the outer scope even when the function that created // has terminated const greeting = "Good Morning"; let displayGreeting; function greets(person) { const fullName = person.name + " " + person.surname; return function () { console.log(greeting + " " + fullName); }; } displayGreeting = greets({ name: "Ryan", surname: "Ray" }); displayGreeting(); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |