├── .gitattributes ├── .gitignore ├── Chapter 10 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt ├── Code09.txt ├── Code10.txt ├── Code11.txt ├── Code12.txt ├── Code13.txt ├── Code14.txt ├── Code15.txt ├── Code16.txt ├── Code17.txt ├── Code18.txt ├── Code19.txt ├── Code20.txt ├── Code21.txt ├── Code22.txt ├── Code23.txt ├── Code24.txt ├── Code25.txt └── Code26.txt ├── Chapter 11 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt └── Code09.txt ├── Chapter 12 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt └── Code06.txt ├── Chapter 2 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt └── Code09.txt ├── Chapter 3 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt ├── Code09.txt ├── Code10.txt ├── Code11.txt ├── Code12.txt ├── Code13.txt ├── Code14.txt ├── Code15.txt ├── Code16.txt ├── Code17.txt └── Code18.txt ├── Chapter 4 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt ├── Code09.txt ├── Code10.txt ├── Code11.txt ├── Code12.txt ├── Code13.txt ├── Code14.txt ├── Code15.txt ├── Code16.txt ├── Code17.txt ├── Code18.txt ├── Code19.txt └── Code20.txt ├── Chapter 5 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt ├── Code09.txt ├── Code10.txt ├── Code11.txt ├── Code12.txt ├── Code13.txt ├── Code14.txt └── Code15.txt ├── Chapter 6 ├── Code01.txt ├── Code02.txt ├── Code03.txt ├── Code04.txt ├── Code05.txt ├── Code06.txt ├── Code07.txt ├── Code08.txt ├── Code09.txt └── Code10.txt ├── Chapter 7 ├── Code01.html ├── Code02.html ├── Code03.html ├── Code04.html ├── Code05.html ├── Code06.html └── Code07.html ├── Chapter 8 ├── Code01.html ├── Code02.html ├── Code03.html ├── Code04.html ├── Code05.html ├── Code06.html ├── Code07.html └── Code08.html ├── Chapter 9 ├── Code01.html ├── Code02.html ├── Code03.html ├── Code04.html ├── Code05.html ├── Code06.html ├── Code07.html ├── Code08.html ├── Code09.html ├── Code10.html ├── Code11.html └── Code12.html └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter 10/Code01.txt: -------------------------------------------------------------------------------- 1 | var myApplication = { 2 | version: "1.0", 3 | name: "My Application", 4 | config: {/*...*/}, 5 | init: function() {/*...*/} 6 | }; 7 | 8 | console.log(myApplication.name); //My Application 9 | myApplication.init(); 10 | -------------------------------------------------------------------------------- /Chapter 10/Code02.txt: -------------------------------------------------------------------------------- 1 | var myApplication = myApplication || {}; 2 | myApplication = { 3 | version: "1.0", 4 | name: "My Application", 5 | config: {/*...*/}, 6 | init: function() {/*...*/} 7 | }; 8 | -------------------------------------------------------------------------------- /Chapter 10/Code03.txt: -------------------------------------------------------------------------------- 1 | var myApplication = { 2 | version: "1.0", 3 | name: "My Application", 4 | config: { 5 | ui: { 6 | backgroundColor: "green", 7 | fontSize: 12 8 | }, 9 | localization: { 10 | language: "english", 11 | dateFormat: "MM-dd-yyyy" 12 | } 13 | }, 14 | init: function() {/*...*/} 15 | }; -------------------------------------------------------------------------------- /Chapter 10/Code04.txt: -------------------------------------------------------------------------------- 1 | var myApplication = { 2 | version: "1.0", 3 | name: "My Application", 4 | init: function() {/*...*/} 5 | }; 6 | 7 | myApplication.config: {}; 8 | myApplication.config.ui: { 9 | backgroundColor: "green", 10 | fontSize: 12 11 | }; 12 | myApplication.config.localization: { 13 | language: "english", 14 | dateFormat: "MM-dd-yyyy" 15 | }; 16 | 17 | myApplication.config.localization.language: "italian", 18 | myApplication.config.localization.dateFormat = "dd-MM-yyyy"; -------------------------------------------------------------------------------- /Chapter 10/Code05.txt: -------------------------------------------------------------------------------- 1 | var myApplication = {}; 2 | 3 | (function(nameSpace) { 4 | nameSpace.version = "1.0", 5 | nameSpace.name = "My Application", 6 | nameSpace.config = {/*...*/}, 7 | nameSpace.init = function() {/*...*/} 8 | })(myApplication); 9 | 10 | -------------------------------------------------------------------------------- /Chapter 10/Code06.txt: -------------------------------------------------------------------------------- 1 | var myApplication = {}; 2 | 3 | (function() { 4 | this.version: "1.0", 5 | this.name: "My Application", 6 | this.config: {/*...*/}, 7 | this.init: function() {/*...*/} 8 | }).apply(myApplication); -------------------------------------------------------------------------------- /Chapter 10/Code07.txt: -------------------------------------------------------------------------------- 1 | var geoModule = (function() { 2 | var pi = 3.14; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*radius*radius; 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | })(); 17 | 18 | console.log(geoModule.calculateCircumference (5)); 19 | //result: 31.400000000000002 20 | 21 | console.log(geoModule.calculateCircleArea(5)); 22 | //result: 78.5 -------------------------------------------------------------------------------- /Chapter 10/Code08.txt: -------------------------------------------------------------------------------- 1 | var myModule = (function() { 2 | function Person(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | 7 | return { 8 | Person: Person 9 | }; 10 | })(); 11 | 12 | var johnSmith = new myModule.Person("John", "Smith"); 13 | -------------------------------------------------------------------------------- /Chapter 10/Code09.txt: -------------------------------------------------------------------------------- 1 | var geoModule = (function(mathModule) { 2 | var pi = mathModule.PI; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*mathModule.pow(radius, 2); 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | })(Math); 17 | 18 | console.log(geoModule.calculateCircumference(5)); 19 | //result: 31.41592653589793 20 | 21 | console.log(geoModule.calculateCircleArea(5)); 22 | //result: 78.53981633974483 23 | -------------------------------------------------------------------------------- /Chapter 10/Code10.txt: -------------------------------------------------------------------------------- 1 | var geoModule = (function(mathModule) { 2 | var pi = mathModule.PI; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*mathModule.pow(radius, 2); 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | })(Math); 17 | 18 | geoModule = (function(mathModule, me) { 19 | me.calculateSphereVolume = function(radius) { 20 | return 4*mathModule.PI*mathModule.pow(radius, 2); 21 | }; 22 | 23 | return me; 24 | })(Math, geoModule); 25 | 26 | 27 | console.log(geoModule.calculateCircumference (5)); 28 | //result: 31.41592653589793 29 | 30 | console.log(geoModule.calculateCircleArea(5)); 31 | //result: 78.53981633974483 32 | 33 | console.log(geoModule.calculateSphereVolume(5)); 34 | //result: 314.1592653589793 -------------------------------------------------------------------------------- /Chapter 10/Code11.txt: -------------------------------------------------------------------------------- 1 | geoModule = (function(mathModule, me) { 2 | me.calculateSphereVolume = function(radius) { 3 | return 4*mathModule.PI*mathModule.pow(radius, 2); 4 | }; 5 | 6 | return me; 7 | })(Math, geoModule || {}); -------------------------------------------------------------------------------- /Chapter 10/Code12.txt: -------------------------------------------------------------------------------- 1 | var geoModule = (function(mathModule) { 2 | var pi = mathModule.PI; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*mathModule.pow(radius, 2); 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | })(Math); 17 | 18 | geoModule = (function(me) { 19 | var oldCalculateCircleArea = me.calculateCircleArea; 20 | 21 | me.calculateCircleArea = function(radius) { 22 | return oldCalculateCircleArea(radius).toFixed(2); 23 | }; 24 | 25 | return me; 26 | })(geoModule); 27 | 28 | console.log(geoModule.calculateCircleArea(5)); 29 | //result: 78.54 -------------------------------------------------------------------------------- /Chapter 10/Code13.txt: -------------------------------------------------------------------------------- 1 | var geoModule = (function(mathModule) { 2 | var pi = mathModule.PI; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*mathModule.pow(radius, 2); 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | })(Math); 17 | 18 | geoModule.triangleModule = (function() { 19 | 20 | function perimeter(side1, side2, side3) { 21 | return side1+side2+side3; 22 | } 23 | 24 | function area(basis, height) { 25 | return basis*height/2; 26 | } 27 | 28 | return { 29 | calculateTrianglePerimeter: perimeter, 30 | calculateTriangleArea: area 31 | }; 32 | 33 | })(); 34 | 35 | geoModule.triangleModule.calculateTriangleArea(3,4); //result: 6 -------------------------------------------------------------------------------- /Chapter 10/Code14.txt: -------------------------------------------------------------------------------- 1 | function getFileContent(moduleName) { /*...*/ } 2 | 3 | function loadModule(moduleName) { 4 | var moduleCode = new Function("return " + getFileContent(moduleName)); 5 | 6 | return moduleCode(); 7 | } -------------------------------------------------------------------------------- /Chapter 10/Code15.txt: -------------------------------------------------------------------------------- 1 | function getFileContent(moduleName) { /*...*/ } 2 | 3 | function loadModule(moduleName) { 4 | var moduleCode; 5 | var module; 6 | 7 | if (moduleName in loadModule.cache) { 8 | module = loadModule.cache[moduleName]; 9 | } else { 10 | moduleCode = new Function("return " + getFileContent(moduleName)); 11 | module = moduleCode(); 12 | loadModule.cache[moduleName] = module; 13 | } 14 | 15 | return module; 16 | } 17 | 18 | loadModule.cache = {}; -------------------------------------------------------------------------------- /Chapter 10/Code16.txt: -------------------------------------------------------------------------------- 1 | var declaration = "var x = 123;"; 2 | 3 | function evaluate(code) { 4 | eval(code); 5 | return x; 6 | } 7 | 8 | console.log(evaluate(declaration)); //result: 123 9 | console.log(x); //result: undefined -------------------------------------------------------------------------------- /Chapter 10/Code17.txt: -------------------------------------------------------------------------------- 1 | var declaration = "var x = 123;"; 2 | 3 | function evaluate(code) { 4 | window.eval(code); 5 | return x; 6 | } 7 | 8 | console.log(evaluate(declaration)); //result: 123 9 | console.log(x); //result: 123 -------------------------------------------------------------------------------- /Chapter 10/Code18.txt: -------------------------------------------------------------------------------- 1 | var pi = 3.14; 2 | 3 | function circumference(radius) { 4 | return 2*pi*radius; 5 | } 6 | 7 | function circleArea(radius) { 8 | return pi*radius*radius; 9 | } 10 | 11 | module.exports = { 12 | calculateCircumference: circumference, 13 | calculateCircleArea: circleArea 14 | }; 15 | -------------------------------------------------------------------------------- /Chapter 10/Code19.txt: -------------------------------------------------------------------------------- 1 | var geoModule = require("./geoModule"); 2 | 3 | console.log(geoModule.calculateCircumference(5)); 4 | -------------------------------------------------------------------------------- /Chapter 10/Code20.txt: -------------------------------------------------------------------------------- 1 | define("geoModule", [], function() { 2 | var pi = 3.14; 3 | 4 | function circumference(radius) { 5 | return 2*pi*radius; 6 | } 7 | 8 | function circleArea(radius) { 9 | return pi*radius*radius; 10 | } 11 | 12 | return { 13 | calculateCircumference: circumference, 14 | calculateCircleArea: circleArea 15 | }; 16 | 17 | }); 18 | -------------------------------------------------------------------------------- /Chapter 10/Code21.txt: -------------------------------------------------------------------------------- 1 | require(["geoModule"], function(geoModule) { 2 | console.log(geoModule.calculateCircumference(5)); 3 | }); 4 | -------------------------------------------------------------------------------- /Chapter 10/Code22.txt: -------------------------------------------------------------------------------- 1 | function requireWithPromise(modules) { 2 | return new Promise(function(resolve, reject) { 3 | try { 4 | require(modules, resolve); 5 | } 6 | catch(e) { 7 | reject(new Error(e.message)); 8 | } 9 | }); 10 | } 11 | 12 | requireWithPromise(["geoModule"]) 13 | .then(function(geoModule) { 14 | console.log(geoModule.calculateCircumference(5)); 15 | }) 16 | .catch(function(error) { console.log(error.message); }); 17 | -------------------------------------------------------------------------------- /Chapter 10/Code23.txt: -------------------------------------------------------------------------------- 1 | (function() { 2 | var pi = 3.14; 3 | var export; 4 | 5 | function circumference(radius) { 6 | return 2*pi*radius; 7 | } 8 | 9 | function circleArea(radius) { 10 | return pi*radius*radius; 11 | } 12 | 13 | export = { 14 | calculateCircumference: circumference, 15 | calculateCircleArea: circleArea 16 | }; 17 | 18 | if (typeof define === 'function') { 19 | define([], function () { 20 | return export; 21 | }); 22 | } 23 | })(); 24 | -------------------------------------------------------------------------------- /Chapter 10/Code24.txt: -------------------------------------------------------------------------------- 1 | (function (root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(); 6 | } else { 7 | root.returnExports = factory(); 8 | } 9 | }(this, function () { 10 | var pi = 3.14; 11 | 12 | function circumference(radius) { 13 | return 2*pi*radius; 14 | } 15 | 16 | function circleArea(radius) { 17 | return pi*radius*radius; 18 | } 19 | 20 | return { 21 | calculateCircumference: circumference, 22 | calculateCircleArea: circleArea 23 | }; 24 | })); -------------------------------------------------------------------------------- /Chapter 10/Code25.txt: -------------------------------------------------------------------------------- 1 | var pi = 3.14; 2 | 3 | export function circumference(radius) { 4 | return 2*pi*radius; 5 | } 6 | 7 | export function circleArea(radius) { 8 | return pi*radius*radius; 9 | } -------------------------------------------------------------------------------- /Chapter 10/Code26.txt: -------------------------------------------------------------------------------- 1 | import {circumference} from "geoModule"; 2 | 3 | console.log(circumference(5)); 4 | -------------------------------------------------------------------------------- /Chapter 11/Code01.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.items = []; 5 | } 6 | 7 | var OrderManager = (function () { 8 | 9 | function OrderManager() {} 10 | 11 | OrderManager.prototype.createOrder = function (customerId) { 12 | this.order = new Order(customerId); 13 | }; 14 | 15 | OrderManager.prototype.addItem = function (item) { 16 | this.order.items.push(item); 17 | }; 18 | 19 | OrderManager.prototype.sendOrder = function () { 20 | if (this.isValid(this.order)) { 21 | var xhr = new XMLHttpRequest(); 22 | xhr.onreadystatechange = function () { 23 | if (xhr.readyState == 4 && xhr.status == 200) { 24 | var response = JSON.parse(xhr.responseText); 25 | handleResponse(response); 26 | } 27 | }; 28 | xhr.open("POST", "/api/orders"); 29 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 30 | xhr.send(JSON.stringify(order)); 31 | } 32 | else { 33 | handleError({ message: "Not valid order!" }); 34 | } 35 | }; 36 | 37 | OrderManager.prototype.isValid = function (order) { 38 | return order.items.length > 0; 39 | }; 40 | 41 | return OrderManager; 42 | }()); 43 | 44 | 45 | 46 | 47 | function handleResponse(response) { 48 | console.log(JSON.stringify(response)); 49 | } 50 | 51 | function handleError(error) { 52 | console.log(error.message); 53 | } -------------------------------------------------------------------------------- /Chapter 11/Code02.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.items = []; 5 | } 6 | 7 | 8 | var OrderManager = (function () { 9 | 10 | function OrderManager() {} 11 | 12 | OrderManager.prototype.createOrder = function (customerId) { 13 | this.order = new Order(customerId); 14 | }; 15 | 16 | OrderManager.prototype.addItem = function (item) { 17 | this.order.items.push(item); 18 | }; 19 | 20 | OrderManager.prototype.sendOrder = function () { 21 | if (this.isValid(this.order)) { 22 | var orderSender = new OrderSender(); 23 | orderSender.send(order); 24 | } 25 | else { 26 | handleError({ message: "Not valid order!" }); 27 | } 28 | }; 29 | 30 | OrderManager.prototype.isValid = function (order) { 31 | return order.items.length > 0; 32 | }; 33 | 34 | return OrderManager; 35 | }()); 36 | 37 | 38 | var OrderSender = (function() { 39 | 40 | function OrderSender() {} 41 | 42 | OrderSender.prototype.send = function(order) { 43 | var xhr = new XMLHttpRequest(); 44 | 45 | xhr.onreadystatechange = function () { 46 | if (xhr.readyState == 4 && xhr.status == 200) { 47 | var response = JSON.parse(xhr.responseText); 48 | handleResponse(response); 49 | } 50 | }; 51 | 52 | xhr.open("POST", "/api/orders"); 53 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 54 | xhr.send(JSON.stringify(order)); 55 | } 56 | 57 | return OrderSender; 58 | })(); 59 | 60 | 61 | 62 | 63 | function handleResponse(response) { 64 | console.log(JSON.stringify(response)); 65 | } 66 | 67 | function handleError(error) { 68 | console.log(error.message); 69 | } 70 | -------------------------------------------------------------------------------- /Chapter 11/Code03.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.totalAmount = 0; 5 | this.items = []; 6 | } 7 | 8 | 9 | var OrderManager = (function () { 10 | 11 | function OrderManager() {} 12 | 13 | OrderManager.prototype.createOrder = function (customerId) { 14 | this.order = new Order(customerId); 15 | }; 16 | 17 | OrderManager.prototype.addItem = function (item) { 18 | this.order.items.push(item); 19 | this.order.totalAmount = this.order.totalAmount + item.price; 20 | }; 21 | 22 | OrderManager.prototype.sendOrder = function () { 23 | if (this.isValid(this.order)) { 24 | this.applyDiscount(this.order); 25 | var orderSender = new OrderSender(); 26 | orderSender.send(order); 27 | } 28 | else { 29 | handleError({ message: "Not valid order!" }); 30 | } 31 | }; 32 | 33 | OrderManager.prototype.isValid = function (order) { 34 | return order.items.length > 0; 35 | }; 36 | 37 | OrderManager.prototype.applyDiscount = function (order) { 38 | var itemsCount = order.items.length; 39 | var discountPercentage; 40 | 41 | if (itemsCount < 10) { 42 | discountPercentage = 0; 43 | } else { 44 | if (itemsCount < 20) { 45 | discountPercentage = 10; 46 | } else { 47 | if (itemsCount < 30) { 48 | discountPercentage = 30; 49 | } else { 50 | discountPercentage = 50; 51 | } 52 | } 53 | } 54 | 55 | order.totalAmount = order.totalAmount - order.totalAmount * discountPercentage / 100; 56 | }; 57 | 58 | return OrderManager; 59 | }()); 60 | 61 | 62 | 63 | function handleResponse(response) { 64 | console.log(JSON.stringify(response)); 65 | } 66 | 67 | function handleError(error) { 68 | console.log(error.message); 69 | } 70 | -------------------------------------------------------------------------------- /Chapter 11/Code04.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.totalAmount = 0; 5 | this.items = []; 6 | } 7 | 8 | 9 | var OrderManager = (function () { 10 | var discounters = []; 11 | 12 | function OrderManager() {} 13 | 14 | OrderManager.prototype.createOrder = function (customerId) { 15 | this.order = new Order(customerId); 16 | }; 17 | 18 | OrderManager.prototype.addItem = function (item) { 19 | this.order.items.push(item); 20 | this.order.totalAmount = this.order.totalAmount + item.price; 21 | }; 22 | 23 | OrderManager.prototype.sendOrder = function () { 24 | if (this.isValid(this.order)) { 25 | this.applyDiscount(this.order); 26 | var orderSender = new OrderSender(); 27 | orderSender.send(order); 28 | } 29 | else { 30 | handleError({ message: "Not valid order!" }); 31 | } 32 | }; 33 | 34 | OrderManager.prototype.isValid = function (order) { 35 | return order.items.length > 0; 36 | }; 37 | 38 | OrderManager.prototype.registerDiscounter = function(discounter) { 39 | discounters.push(discounter); 40 | }; 41 | 42 | OrderManager.prototype.applyDiscount = function (order) { 43 | var i; 44 | 45 | for (i=0; i < discounters.length; i++) { 46 | if (discounters[i].isApplicable(order)) { 47 | discounters[i].apply(order); 48 | break 49 | } 50 | } 51 | }; 52 | 53 | return OrderManager; 54 | }()); 55 | 56 | 57 | var bronzeDiscounter = { 58 | isApplicable: function(order) { 59 | var itemsCount = order.items.length; 60 | 61 | return (itemsCount >= 10 && itemsCount < 20) 62 | }, 63 | apply: function(order) { 64 | order.totalAmount = order.totalAmount - order.totalAmount * 10 / 100; 65 | } 66 | }; 67 | 68 | var silverDiscounter = { 69 | isApplicable: function(order) { 70 | var itemsCount = order.items.length; 71 | 72 | return (itemsCount >= 20 && itemsCount < 30) 73 | }, 74 | apply: function(order) { 75 | order.totalAmount = order.totalAmount - order.totalAmount * 30 / 100; 76 | } 77 | }; 78 | 79 | var goldDiscounter = { 80 | isApplicable: function(order) { 81 | var itemsCount = order.items.length; 82 | 83 | return (itemsCount >= 30) 84 | }, 85 | apply: function(order) { 86 | order.totalAmount = order.totalAmount - order.totalAmount * 50 / 100; 87 | } 88 | }; 89 | 90 | 91 | function handleResponse(response) { 92 | console.log(JSON.stringify(response)); 93 | } 94 | 95 | function handleError(error) { 96 | console.log(error.message); 97 | } 98 | -------------------------------------------------------------------------------- /Chapter 11/Code05.txt: -------------------------------------------------------------------------------- 1 | function Discounter(min, max, discountPercentage) { 2 | this.min = min; 3 | this.max = max; 4 | this.discountPercentage = discountPercentage; 5 | } 6 | 7 | Discounter.prototype.isApplicable = function(order) { 8 | var itemsCount = order.items.length; 9 | 10 | return (itemsCount >= this.min && itemsCount < this.max) 11 | }; 12 | 13 | Discounter.prototype.apply = function(order) { 14 | order.totalAmount = order.totalAmount - order.totalAmount * discountPercentage / 100; 15 | }; 16 | 17 | 18 | 19 | function AmountDiscounter(min, max, discountPercentage) { 20 | Discounter.apply(this, arguments); 21 | } 22 | 23 | AmountDiscounter.prototype.isApplicable = function(order) { 24 | var orderAmount = order.totalAmount; 25 | 26 | return (orderAmount >= min && orderAmount < max) 27 | }; -------------------------------------------------------------------------------- /Chapter 11/Code06.txt: -------------------------------------------------------------------------------- 1 | function Discounter(min, max, discountPercentage, gadget) { 2 | this.min = min; 3 | this.max = max; 4 | this.discountPercentage = discountPercentage; 5 | this.gadget = gadget; 6 | } 7 | 8 | Discounter.prototype.isApplicable = function(order) { 9 | var itemsCount = order.items.length; 10 | 11 | return (itemsCount >= this.min && itemsCount < this.max) 12 | }; 13 | 14 | Discounter.prototype.apply = function(order) { 15 | order.totalAmount = order.totalAmount - order.totalAmount * discountPercentage / 100; 16 | }; 17 | 18 | Discounter.prototype.addGadget = function(order) { 19 | order.items.push(this.gadget); 20 | } 21 | -------------------------------------------------------------------------------- /Chapter 11/Code07.txt: -------------------------------------------------------------------------------- 1 | function Discounter(min, max, discountPercentage) { 2 | this.min = min; 3 | this.max = max; 4 | this.discountPercentage = discountPercentage; 5 | } 6 | 7 | Discounter.prototype.isApplicable = function(order) { 8 | var itemsCount = order.items.length; 9 | 10 | return (itemsCount >= this.min && itemsCount < this.max) 11 | }; 12 | 13 | Discounter.prototype.apply = function(order) { 14 | order.totalAmount = order.totalAmount - order.totalAmount * discountPercentage / 100; 15 | }; 16 | 17 | var gadgetMixin = { 18 | gadget: {}, 19 | addGadget: function(order) { 20 | order.items.push(this.gadget); 21 | } 22 | }; 23 | 24 | var discounter = new Discounter(10, 20, 0); 25 | var gadgetDiscounter = augment(discounter, gadgetMixin); 26 | 27 | gadgetDiscounter.gadget = {name: "A nice gadget!"} 28 | 29 | 30 | function augment(destination, source) { 31 | for (var methodName in source) { 32 | if (source.hasOwnProperty(methodName)) { 33 | destination[methodName] = source[methodName]; 34 | } 35 | } 36 | return destination; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter 11/Code08.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.totalAmount = 0; 5 | this.items = []; 6 | } 7 | 8 | 9 | var OrderManager = (function () { 10 | var discounters = []; 11 | 12 | function OrderManager() {} 13 | 14 | OrderManager.prototype.createOrder = function (customerId) { 15 | this.order = new Order(customerId); 16 | }; 17 | 18 | OrderManager.prototype.addItem = function (item) { 19 | this.order.items.push(item); 20 | this.order.totalAmount = this.order.totalAmount + item.price; 21 | }; 22 | 23 | OrderManager.prototype.sendOrder = function () { 24 | if (this.isValid(this.order)) { 25 | this.applyDiscount(this.order); 26 | var orderSender = new OrderSender(); 27 | orderSender.send(order); 28 | } 29 | else { 30 | handleError({ message: "Not valid order!" }); 31 | } 32 | }; 33 | 34 | OrderManager.prototype.isValid = function (order) { 35 | return order.items.length > 0; 36 | }; 37 | 38 | OrderManager.prototype.registerDiscounter = function(discounter) { 39 | discounters.push(discounter); 40 | }; 41 | 42 | OrderManager.prototype.applyDiscount = function (order) { 43 | var i; 44 | 45 | for (i=0; i < discounters.length; i++) { 46 | if (discounters[i].isApplicable(order)) { 47 | discounters[i].apply(order); 48 | break 49 | } 50 | } 51 | }; 52 | 53 | return OrderManager; 54 | }()); 55 | 56 | var OrderSender = (function() { 57 | 58 | function OrderSender() {} 59 | 60 | OrderSender.prototype.send = function(order) { 61 | var xhr = new XMLHttpRequest(); 62 | 63 | xhr.onreadystatechange = function () { 64 | if (xhr.readyState == 4 && xhr.status == 200) { 65 | var response = JSON.parse(xhr.responseText); 66 | handleResponse(response); 67 | } 68 | }; 69 | 70 | xhr.open("POST", "/api/orders"); 71 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 72 | xhr.send(JSON.stringify(order)); 73 | } 74 | 75 | return OrderSender; 76 | })(); -------------------------------------------------------------------------------- /Chapter 11/Code09.txt: -------------------------------------------------------------------------------- 1 | function Order(customerId) { 2 | this.customerId = customerId; 3 | this.dateTime = new Date(); 4 | this.totalAmount = 0; 5 | this.items = []; 6 | } 7 | 8 | 9 | var OrderManager = (function () { 10 | var discounters = []; 11 | var orderSender; 12 | 13 | function OrderManager(sender) { 14 | orderSender = sender; 15 | } 16 | 17 | OrderManager.prototype.createOrder = function (customerId) { 18 | this.order = new Order(customerId); 19 | }; 20 | 21 | OrderManager.prototype.addItem = function (item) { 22 | this.order.items.push(item); 23 | this.order.totalAmount = this.order.totalAmount + item.price; 24 | }; 25 | 26 | OrderManager.prototype.sendOrder = function () { 27 | if (this.isValid(this.order)) { 28 | this.applyDiscount(this.order); 29 | orderSender.send(order); 30 | } 31 | else { 32 | handleError({ message: "Not valid order!" }); 33 | } 34 | }; 35 | 36 | OrderManager.prototype.isValid = function (order) { 37 | return order.items.length > 0; 38 | }; 39 | 40 | OrderManager.prototype.registerDiscounter = function(discounter) { 41 | discounters.push(discounter); 42 | }; 43 | 44 | OrderManager.prototype.applyDiscount = function (order) { 45 | var i; 46 | 47 | for (i=0; i < discounters.length; i++) { 48 | if (discounters[i].isApplicable(order)) { 49 | discounters[i].apply(order); 50 | break 51 | } 52 | } 53 | }; 54 | 55 | return OrderManager; 56 | }()); 57 | 58 | 59 | 60 | var HttpOrderSender = (function() { 61 | 62 | function OrderSender() {} 63 | 64 | OrderSender.prototype.send = function(order) { 65 | var xhr = new XMLHttpRequest(); 66 | 67 | xhr.onreadystatechange = function () { 68 | if (xhr.readyState == 4 && xhr.status == 200) { 69 | var response = JSON.parse(xhr.responseText); 70 | handleResponse(response); 71 | } 72 | }; 73 | 74 | xhr.open("POST", "/api/orders"); 75 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 76 | xhr.send(JSON.stringify(order)); 77 | } 78 | 79 | return OrderSender; 80 | })(); -------------------------------------------------------------------------------- /Chapter 12/Code01.txt: -------------------------------------------------------------------------------- 1 | function Facade() {} 2 | 3 | Facade.prototype.getElement = function(selector) { 4 | return document.querySelector(selector); 5 | }; 6 | 7 | var facade = new Facade(); 8 | var myElement = facade.getElement(".class"); 9 | -------------------------------------------------------------------------------- /Chapter 12/Code02.txt: -------------------------------------------------------------------------------- 1 | var Mediator = function() { 2 | 3 | var colleagues = {}; 4 | 5 | return { 6 | register: function(colleague) { 7 | colleagues[colleague.name] = colleague; 8 | colleague.mediator = this; 9 | }, 10 | 11 | send: function(message, sender, receiver) { 12 | if (receiver) { 13 | receiver.receive(message, sender); 14 | } else { 15 | for (key in colleagues) { 16 | if (colleagues[key] != sender) { 17 | colleagues[key].receive(message, sender); 18 | } 19 | } 20 | } 21 | } 22 | }; 23 | }; 24 | 25 | 26 | var Colleague = function(name) { 27 | this.name = name; 28 | this.mediator = null; 29 | }; 30 | 31 | Colleague.prototype.send = function(message, receiver) { 32 | this.mediator.send(message, this, receiver); 33 | }; 34 | 35 | Colleague.prototype.receive = function(message, sender) { 36 | //process the message 37 | }; 38 | 39 | 40 | var mediator = new Mediator(); 41 | var johnSmith = new Colleague("John"); 42 | var marioRossi = new Colleague("Mario"); 43 | 44 | mediator.register(johnSmith); 45 | mediator.register(marioRossi); 46 | 47 | 48 | johnSmith.send("Hello!", marioRossi); 49 | 50 | johnSmith.send("Hello!"); -------------------------------------------------------------------------------- /Chapter 12/Code03.txt: -------------------------------------------------------------------------------- 1 | function Calculator() {} 2 | 3 | Calculator.prototype.sum = function(x, y) { 4 | var result = x + y; 5 | 6 | return result; 7 | }; 8 | -------------------------------------------------------------------------------- /Chapter 12/Code04.txt: -------------------------------------------------------------------------------- 1 | function Calculator() {} 2 | 3 | Calculator.prototype.sum = function(x, y) { 4 | console.log("Calling sum on " + x + " and " + y); 5 | var result = x + y; 6 | 7 | console.log("Result of sum is " + result); 8 | return result; 9 | }; -------------------------------------------------------------------------------- /Chapter 12/Code05.txt: -------------------------------------------------------------------------------- 1 | function LoggedCalculator() { 2 | Calculator.apply(this, arguments); 3 | } 4 | 5 | 6 | LoggedCalculator.prototype.sum = function(x, y) { 7 | console.log("Calling sum on " + x + " and " + y); 8 | var result = Calculator.prototype.sum(x, y); 9 | 10 | console.log("Result of sum is " + result); 11 | return result; 12 | }; -------------------------------------------------------------------------------- /Chapter 12/Code06.txt: -------------------------------------------------------------------------------- 1 | var originalSum = Calculator.prototype.sum; 2 | 3 | Calculator.prototype.sum = function(x, y) { 4 | console.log("Calling sum on " + x + " and " + y); 5 | var result = originalSum(x, y); 6 | 7 | console.log("Result of sum is " + result); 8 | return result; 9 | }; -------------------------------------------------------------------------------- /Chapter 2/Code01.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | this.parent = null; 5 | } 6 | 7 | var johnSmith = new Person("John", "Smith"); 8 | var fredSmith = new Person("Fred", "Smith"); 9 | 10 | fredSmith.parent = johnSmith; -------------------------------------------------------------------------------- /Chapter 2/Code02.txt: -------------------------------------------------------------------------------- 1 | var company = { 2 | name: "ACME Inc.", 3 | employees: [] 4 | }; 5 | 6 | var johnSmith = new Person("John", "Smith"); 7 | var marioRossi = new Person("Mario", "Rossi"); 8 | 9 | company.employees.push(johnSmith); 10 | company.employees.push(marioRossi); -------------------------------------------------------------------------------- /Chapter 2/Code03.txt: -------------------------------------------------------------------------------- 1 | function Company(name) { 2 | var employees = []; 3 | 4 | this.name = name; 5 | 6 | this.prototype.getEmployees = function() { 7 | return employees; 8 | }; 9 | this.prototype.addEmployee = function(employee) { 10 | employees.push(employee); 11 | }; 12 | this.prototype.sortEmployeesByName = function() { 13 | ... 14 | }; 15 | } 16 | 17 | var company = new Company("ACME Inc."); -------------------------------------------------------------------------------- /Chapter 2/Code04.txt: -------------------------------------------------------------------------------- 1 | //C# 2 | 3 | public int Sum(int x, int y) { 4 | return Sum(x, y, 0); 5 | } 6 | 7 | public int Sum(int x, int y, int z) { 8 | return x+ y + z; 9 | } -------------------------------------------------------------------------------- /Chapter 2/Code05.txt: -------------------------------------------------------------------------------- 1 | function sum(x, y, z) { 2 | x = x?x:0; 3 | y = y?y:0; 4 | z = z?z:0; 5 | return x + y + z; 6 | } -------------------------------------------------------------------------------- /Chapter 2/Code06.txt: -------------------------------------------------------------------------------- 1 | //C# 2 | 3 | public class Stack { 4 | private T[] items; 5 | private int count; 6 | public void Push(T item) { ... } 7 | public T Pop() { ... } 8 | } 9 | 10 | var stack = new Stack(); -------------------------------------------------------------------------------- /Chapter 2/Code07.txt: -------------------------------------------------------------------------------- 1 | function Stack() 2 | { 3 | this.stack = []; 4 | this.pop = function(){ 5 | return this.stack.pop(); 6 | } 7 | this.push = function(item){ 8 | this.stack.push(item); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 2/Code08.txt: -------------------------------------------------------------------------------- 1 | //C# 2 | 3 | public class Person { 4 | public string Name {get; set;} 5 | public string SurName {get; set;} 6 | } 7 | 8 | public class Programmer:Person { 9 | public String KnownLanguage {get; set;} 10 | } 11 | 12 | public void WriteFullName(Person p) { 13 | Console.WriteLine(p.Name + " " + p.SurName); 14 | } 15 | 16 | var a = new Person(); 17 | a.Name = "John"; 18 | a.SurName = "Smith"; 19 | 20 | var b = new Programmer(); 21 | b.Name = "Mario"; 22 | b.SurName = "Rossi"; 23 | b.KnownLanguage = "C#"; 24 | 25 | WriteFullName(a); //result: John Smith 26 | WriteFullName(b); //result: Mario Rossi -------------------------------------------------------------------------------- /Chapter 2/Code09.txt: -------------------------------------------------------------------------------- 1 | function Person() { 2 | this.name = ""; 3 | this.surname = ""; 4 | } 5 | 6 | function Programmer() { 7 | this.knownLanguage = ""; 8 | } 9 | 10 | Programmer.prototype = new Person(); 11 | 12 | function writeFullName(p) { 13 | console.log(p.name + " " + p.surname); 14 | } 15 | 16 | var a = new Person(); 17 | a.name = "John"; 18 | a.surname = "Smith"; 19 | 20 | var b = new Programmer(); 21 | b.name = "Mario"; 22 | b.surname = "Rossi"; 23 | b.knownLanguage = "JavaScript"; 24 | 25 | writeFullName(a); //result: John Smith 26 | writeFullName(b); //result: Mario Rossi -------------------------------------------------------------------------------- /Chapter 3/Code01.txt: -------------------------------------------------------------------------------- 1 | function TheatreSeats() { 2 | this._seats = []; 3 | } 4 | 5 | TheatreSeats.prototype.placePerson = function(person) { 6 | this._seats.push(person); 7 | }; 8 | 9 | var theatreSeats = new TheatreSeats(); 10 | 11 | theatreSeats.placePerson({name: "John", surname: "Smith"}); 12 | -------------------------------------------------------------------------------- /Chapter 3/Code02.txt: -------------------------------------------------------------------------------- 1 | function TheatreSeats() { 2 | var seats = []; 3 | 4 | this.placePerson = function(person) { 5 | seats.push(person); 6 | }; 7 | } -------------------------------------------------------------------------------- /Chapter 3/Code03.txt: -------------------------------------------------------------------------------- 1 | var greeting = "Good morning"; 2 | 3 | function greets(person) { 4 | 5 | var fullName = person.name + " " + person.surname; 6 | 7 | function displayGreeting() { 8 | console.log(greeting + " " + fullName); 9 | } 10 | 11 | displayGreeting(); 12 | } 13 | 14 | greets({name: "John", surname: "Smith"}); -------------------------------------------------------------------------------- /Chapter 3/Code04.txt: -------------------------------------------------------------------------------- 1 | var greeting = "Good morning"; 2 | var displayGreeting; 3 | 4 | function greets(person) { 5 | 6 | var fullName = person.name + " " + person.surname; 7 | 8 | 9 | return function () { 10 | console.log(greeting + " " + fullName); 11 | } 12 | } 13 | 14 | displayGreeting = greets({name: "John", surname: "Smith"}); 15 | 16 | displayGreeting(); -------------------------------------------------------------------------------- /Chapter 3/Code05.txt: -------------------------------------------------------------------------------- 1 | function TheatreSeats() { 2 | var seats = []; 3 | } 4 | 5 | TheatreSeats.prototype.placePerson = function(person) { 6 | seats.push(person); 7 | }; 8 | 9 | var theatreSeats = new TheatreSeats(); 10 | 11 | theatreSeats.placePerson({name: "John", surname: "Smith"}); //exception -------------------------------------------------------------------------------- /Chapter 3/Code06.txt: -------------------------------------------------------------------------------- 1 | function TheatreSeats() { 2 | var seats = []; 3 | 4 | this.placePerson = function(person) { 5 | seats.push(person); 6 | }; 7 | this.countOccupiedSeats = function() { 8 | return seats.length; 9 | }; 10 | 11 | this.maxSize = 10; 12 | } 13 | 14 | TheatreSeats.prototype.isSoldOut = function() { 15 | return this.countOccupiedSeats () >= this.maxSize; 16 | }; 17 | 18 | TheatreSeats.prototype.countFreeSeats = function() { 19 | return this.maxSize - this.countOccupiedSeats(); 20 | }; 21 | -------------------------------------------------------------------------------- /Chapter 3/Code07.txt: -------------------------------------------------------------------------------- 1 | var TheatreSeats = (function() { 2 | 3 | var seats = []; 4 | 5 | 6 | 7 | function TheatreSeatsConstructor() { 8 | this.maxSize = 10; 9 | } 10 | 11 | 12 | TheatreSeatsConstructor.prototype.placePerson = function(person) { 13 | 14 | seats.push(person); 15 | 16 | }; 17 | 18 | 19 | TheatreSeatsConstructor.prototype.countOccupiedSeats = function() { 20 | 21 | return seats.length; 22 | 23 | }; 24 | 25 | 26 | TheatreSeatsConstructor.prototype.isSoldOut = function() { 27 | 28 | return seats.length >= this.maxSize; 29 | 30 | }; 31 | 32 | 33 | TheatreSeatsConstructor.prototype.countFreeSeats = function() { 34 | 35 | return this.maxSize - seats.length; 36 | 37 | }; 38 | 39 | 40 | 41 | return TheatreSeatsConstructor; 42 | 43 | 44 | }()); 45 | 46 | 47 | var t1 = new TheatreSeats(); 48 | var t2 = new TheatreSeats(); 49 | 50 | t1.placePerson({name: "John", surname: "Smith"}); 51 | 52 | console.log(t1.countFreeSeats()); //result: 9 53 | console.log(t2.countFreeSeats()); //result: 9 54 | 55 | -------------------------------------------------------------------------------- /Chapter 3/Code08.txt: -------------------------------------------------------------------------------- 1 | var TheatreSeats = (function() { 2 | var priv = {}; 3 | var id = 0; 4 | 5 | function TheatreSeatsConstructor() { 6 | this.id = id++; 7 | this.maxSize = 10; 8 | 9 | priv[this.id] = {}; 10 | priv[this.id].seats = []; 11 | } 12 | 13 | TheatreSeatsConstructor.prototype.placePerson = function(person) { 14 | priv[this.id].seats.push(person); 15 | }; 16 | 17 | TheatreSeatsConstructor.prototype.countOccupiedSeats = function() { 18 | return priv[this.id].seats.length; 19 | }; 20 | 21 | TheatreSeatsConstructor.prototype.isSoldOut = function() { 22 | return priv[this.id].seats.length >= this.maxSize; 23 | }; 24 | 25 | TheatreSeatsConstructor.prototype.countFreeSeats = function() { 26 | return this.maxSize - priv[this.id].seats.length; 27 | }; 28 | 29 | return TheatreSeatsConstructor; 30 | 31 | }()); -------------------------------------------------------------------------------- /Chapter 3/Code09.txt: -------------------------------------------------------------------------------- 1 | var myMap = new WeakMap(); 2 | var johnSmith = {name: "John", surname: "Smith"}; 3 | var marioRossi = {name: "Mario", surname: "Rossi"}; 4 | 5 | 6 | myMap.set(johnSmith, "This is John"); 7 | myMap.set(marioRossi, "This is Mario"); 8 | 9 | console.log(myMap.get(marioRossi)); -------------------------------------------------------------------------------- /Chapter 3/Code10.txt: -------------------------------------------------------------------------------- 1 | var TheatreSeats = (function() { 2 | var priv = new WeakMap(); 3 | 4 | function TheatreSeatsConstructor() { 5 | var privateMembers = {seats: []}; 6 | 7 | priv.set(this, privateMembers); 8 | this.maxSize = 10; 9 | 10 | } 11 | 12 | TheatreSeatsConstructor.prototype.placePerson = function(person) { 13 | priv.get(this).seats.push(person); 14 | }; 15 | 16 | TheatreSeatsConstructor.prototype.countOccupiedSeats = function() { 17 | return priv.get(this).seats.length; 18 | }; 19 | 20 | TheatreSeatsConstructor.prototype.isSoldOut = function() { 21 | return priv.get(this).seats.length >= this.maxSize; 22 | }; 23 | 24 | TheatreSeatsConstructor.prototype.countFreeSeats = function() { 25 | return this.maxSize - priv.get(this).seats.length; 26 | }; 27 | 28 | return TheatreSeatsConstructor; 29 | 30 | }()); -------------------------------------------------------------------------------- /Chapter 3/Code11.txt: -------------------------------------------------------------------------------- 1 | var TheatreSeats = (function() { 2 | var priv = new WeakMap(); 3 | var _= function(instance) {return priv.get(instance);}; 4 | 5 | function TheatreSeatsConstructor() { 6 | var privateMembers = {seats: []}; 7 | 8 | priv.set(this, privateMembers); 9 | this.maxSize = 10; 10 | 11 | } 12 | 13 | TheatreSeatsConstructor.prototype.placePerson = function(person) { 14 | _(this).seats.push(person); 15 | }; 16 | 17 | TheatreSeatsConstructor.prototype.countOccupiedSeats = function() { 18 | return _(this).seats.length; 19 | }; 20 | 21 | TheatreSeatsConstructor.prototype.isSoldOut = function() { 22 | return _(this).seats.length >= this.maxSize; 23 | }; 24 | 25 | TheatreSeatsConstructor.prototype.countFreeSeats = function() { 26 | return this.maxSize - _(this).seats.length; 27 | }; 28 | 29 | return TheatreSeatsConstructor; 30 | 31 | }()); -------------------------------------------------------------------------------- /Chapter 3/Code12.txt: -------------------------------------------------------------------------------- 1 | var person = { 2 | name: "John", 3 | surname: "Smith", 4 | get fullName() { return this.name + " " + this.surname; }, 5 | email: "john.smith@packtpub.com" 6 | }; 7 | 8 | console.log(person.fullName); //John Smith 9 | 10 | person.fullName = "Mario Rossi"; 11 | 12 | console.log(person.fullName); //John Smith 13 | 14 | -------------------------------------------------------------------------------- /Chapter 3/Code13.txt: -------------------------------------------------------------------------------- 1 | var person = { 2 | name: "John", 3 | surname: "Smith", 4 | get fullName() { return this.name + " " + this.surname; }, 5 | set fullName(value) { 6 | var parts = value.toString().split(" "); 7 | 8 | this.name = parts[0] || ""; 9 | this.surname = parts[1] || ""; 10 | }, 11 | email: "john.smith@packtpub.com" 12 | }; 13 | 14 | console.log(person.fullName); //John Smith 15 | 16 | person.fullName = "Mario Rossi"; 17 | 18 | console.log(person.name); //Mario 19 | console.log(person.surname); //Rossi 20 | console.log(person.fullName); //Mario Rossi -------------------------------------------------------------------------------- /Chapter 3/Code14.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | 3 | function PersonConstructor() { 4 | this.name = ""; 5 | this.surname = ""; 6 | this.email = ""; 7 | 8 | Object.defineProperty( 9 | this, 10 | "fullName", 11 | { 12 | get: function() { return this.name + " " + this.surname;}, 13 | set: function(value) { 14 | var parts = value.toString().split(" "); 15 | 16 | this.name = parts[0] || ""; 17 | this.surname = parts[1] || ""; 18 | } 19 | }); 20 | } 21 | 22 | return PersonConstructor; 23 | 24 | }()); -------------------------------------------------------------------------------- /Chapter 3/Code15.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | 3 | function PersonConstructor() { 4 | var _email = ""; 5 | 6 | this.name = ""; 7 | this.surname = ""; 8 | 9 | Object.defineProperty( 10 | this, 11 | "fullName", 12 | { 13 | get: function() { return this.name + " " + this.surname;}, 14 | set: function(value) { 15 | var parts = value.toString().split(" "); 16 | 17 | this.name = parts[0] || ""; 18 | this.surname = parts[1] || ""; 19 | } 20 | }); 21 | 22 | Object.defineProperty( 23 | this, 24 | "email", 25 | { 26 | get: function() { return _email; }, 27 | set: function(value) { 28 | var emailRegExp = /\w+@\w+\.\w{2,4}/i; 29 | 30 | if (emailRegExp.test(value)) { 31 | _email = value; 32 | } else { 33 | throw new Error("Invalid email address!"); 34 | } 35 | } 36 | }); 37 | } 38 | 39 | return PersonConstructor; 40 | 41 | }()); 42 | 43 | var p = new Person(); 44 | 45 | p.email = "john.smith"; //throws exception 46 | -------------------------------------------------------------------------------- /Chapter 3/Code16.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | var priv = new WeakMap(); 3 | var _= function(instance) {return priv.get(instance);}; 4 | 5 | function PersonConstructor() { 6 | var privateMembers = { email: "" }; 7 | 8 | priv.set(this, privateMembers); 9 | 10 | this.name = ""; 11 | this.surname = ""; 12 | } 13 | 14 | Object.defineProperty( 15 | PersonConstructor.prototype, 16 | "fullName", 17 | { 18 | get: function() { return this.name + " " + this.surname;} 19 | }); 20 | 21 | Object.defineProperty( 22 | PersonConstructor.prototype, 23 | "email", 24 | { 25 | get: function() { return _(this).email; }, 26 | set: function(value) { 27 | var emailRegExp = /\w+@\w+\.\w{2,4}/i; 28 | 29 | if (emailRegExp.test(value)) { 30 | _(this).email = value; 31 | } else { 32 | throw new Error("Invalid email address!"); 33 | } 34 | } 35 | }); 36 | 37 | return PersonConstructor; 38 | 39 | }()); 40 | -------------------------------------------------------------------------------- /Chapter 3/Code17.txt: -------------------------------------------------------------------------------- 1 | var TheatreSeats = (function() { 2 | var priv = new WeakMap(); 3 | var _= function(instance) {return priv.get(instance);}; 4 | 5 | class TheatreSeatsClass { 6 | constructor() { 7 | var privateMembers = {seats: []}; 8 | 9 | priv.set(this, privateMembers); 10 | this.maxSize = 10; 11 | 12 | } 13 | 14 | placePerson(person) { 15 | _(this).seats.push(person); 16 | } 17 | 18 | countOccupiedSeats() { 19 | return _(this).seats.length; 20 | } 21 | 22 | isSoldOut() { 23 | return _(this).seats.length >= this.maxSize; 24 | } 25 | 26 | countFreeSeats() { 27 | return this.maxSize - _(this).seats.length; 28 | } 29 | } 30 | 31 | return TheatreSeatsClass; 32 | 33 | }()); -------------------------------------------------------------------------------- /Chapter 3/Code18.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | var priv = new WeakMap(); 3 | var _= function(instance) {return priv.get(instance);}; 4 | 5 | class PersonClass { 6 | constructor() { 7 | var privateMembers = { email: "" }; 8 | 9 | priv.set(this, privateMembers); 10 | 11 | this.name = ""; 12 | this.surname = ""; 13 | } 14 | 15 | get fullName() { 16 | return this.name + " " + this.surname; 17 | } 18 | 19 | get email() { 20 | return _(this).email; 21 | } 22 | 23 | set email(value) { 24 | var emailRegExp = /\w+@\w+\.\w{2,4}/i; 25 | 26 | if (emailRegExp.test(value)) { 27 | _(this).email = value; 28 | } else { 29 | throw new Error("Invalid email address!"); 30 | } 31 | } 32 | } 33 | 34 | return PersonClass; 35 | 36 | }()); -------------------------------------------------------------------------------- /Chapter 4/Code01.txt: -------------------------------------------------------------------------------- 1 | var person = { name: "John", surname: "Smith"}; 2 | var developer = Object.create(person, 3 | { knownLanguage: 4 | { writable: true, 5 | configurable: true 6 | } 7 | }); -------------------------------------------------------------------------------- /Chapter 4/Code02.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | } 5 | 6 | function Developer(name, surname, knownLanguage) { 7 | Person.apply(this, arguments); 8 | this.knownLanguage = knownLanguage; 9 | } 10 | 11 | var johnSmith = new Developer("John", "Smith", "JavaScript"); 12 | 13 | console.log(johnSmith.name); //result: "John" 14 | console.log(johnSmith.surname); //result: "Smith" 15 | console.log(johnSmith.knownLanguage ); //result: "JavaScript" 16 | 17 | 18 | johnSmith instanceof Developer; //result: true 19 | johnSmith instanceof Person; //result: false 20 | johnSmith instanceof Object; //result: true -------------------------------------------------------------------------------- /Chapter 4/Code03.txt: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | } 7 | 8 | 9 | class Developer extends Person { 10 | constructor(name, surname, knownLanguage) { 11 | super(name, surname); 12 | this. knownLanguage = knownLanguage; 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter 4/Code04.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | } 5 | 6 | function Developer(name, surname, knownLanguage) { 7 | Person.apply(this, arguments); 8 | this.knownLanguage = knownLanguage; 9 | } 10 | 11 | Developer.prototype = Object.create(Person.prototype); 12 | Developer.prototype.constructor = Developer; -------------------------------------------------------------------------------- /Chapter 4/Code05.txt: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | 7 | getFullName() { 8 | return this.name + " " + this.surname; 9 | } 10 | } 11 | 12 | 13 | class Developer extends Person { 14 | constructor(name, surname, knownLanguage) { 15 | super(name, surname); 16 | this. knownLanguage = knownLanguage; 17 | } 18 | 19 | displayCompetency() { 20 | console.log(super.getFullName() + " knows " + this.knownLanguage); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter 4/Code06.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | } 5 | 6 | class Developer extends Person { 7 | constructor(name, surname, knownLanguage) { 8 | super(name, surname); 9 | this.knownLanguage = knownLanguage; 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter 4/Code07.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | } 6 | 7 | Person.prototype.getFullName = function() { 8 | return this.name + " " + this.surname; 9 | } 10 | 11 | function Developer(name, surname, knownLanguage) { 12 | Person.apply(this, arguments); 13 | this.knownLanguage = knownLanguage; 14 | } 15 | 16 | Developer.prototype = Object.create(Person.prototype); 17 | Developer.prototype.constructor = Developer; 18 | Developer.prototype.getFullName = function() { 19 | return "Dev " + Person.prototype.getFullName.call(this); 20 | }; 21 | 22 | 23 | var johnSmith = new Person("John", "Smith"); 24 | var marioRossi = new Developer("Mario", "Rossi", "JavaScript"); 25 | 26 | console.log(johnSmith.getFullName()); //result: "John Smith" 27 | console.log(marioRossi.getFullName()); //result: "Dev Mario Rossi" 28 | 29 | -------------------------------------------------------------------------------- /Chapter 4/Code08.txt: -------------------------------------------------------------------------------- 1 | class Developer extends Person { 2 | constructor(name, surname, knownLanguage) { 3 | super(name, surname); 4 | this. knownLanguage = knownLanguage; 5 | } 6 | 7 | getFullName() { 8 | return "Dev "+ super.getFullName(); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 4/Code09.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | } 6 | 7 | Object.defineProperty( 8 | Person.prototype, 9 | "fullName", { 10 | get: function() { return this.name + " " + this.surname; } 11 | }); 12 | 13 | Object.defineProperty( 14 | Developer.prototype, 15 | "fullName", { 16 | get: function() { return "Dev " + this.name + " " + this.surname; } 17 | }); 18 | 19 | 20 | var johnSmith = new Person("John", "Smith"); 21 | var marioRossi = new Developer("Mario", "Rossi", "JavaScript"); 22 | 23 | console.log(johnSmith.fullName); //result: "John Smith" 24 | console.log(marioRossi.fullName); //result: "Dev Mario Rossi" -------------------------------------------------------------------------------- /Chapter 4/Code10.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | function capitalize(string) { 3 | return string.charAt(0).toUpperCase() + string.slice(1); 4 | } 5 | 6 | function PersonConstructor(name, surname) { 7 | this.name = capitalize(name); 8 | this.surname = capitalize(surname); 9 | } 10 | 11 | return PersonConstructor; 12 | }()); -------------------------------------------------------------------------------- /Chapter 4/Code11.txt: -------------------------------------------------------------------------------- 1 | var Person = (function() { 2 | var protectedMembers; 3 | 4 | function capitalize(string) { 5 | return string.charAt(0).toUpperCase() + string.slice(1); 6 | } 7 | 8 | function PersonConstructor(name, surname, protected) { 9 | protectedMembers = protected || {}; 10 | protectedMembers.capitalize = capitalize; 11 | 12 | this.name = capitalize(name); 13 | this.surname = capitalize(surname); 14 | } 15 | 16 | return PersonConstructor; 17 | }()); 18 | 19 | 20 | function Developer(name, surname, knownLanguage) { 21 | var parentProtected = {}; 22 | Person.call(this, name, surname, parentProtected); 23 | 24 | this.knownLanguage = parentProtected.capitalize(knownLanguage); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter 4/Code12.txt: -------------------------------------------------------------------------------- 1 | var person = { name: "John", surname: "Smith"}; 2 | 3 | Object.preventExtensions(person); 4 | 5 | person.age = 32; 6 | 7 | console.log(person.age); //result: undefined 8 | 9 | if (Object.isExtensible(person)) { 10 | person.age = 32; 11 | } 12 | -------------------------------------------------------------------------------- /Chapter 4/Code13.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | Object.preventExtensions(this); 6 | } 7 | 8 | function Developer(name, surname, knownLanguage) { 9 | Person.apply(this, arguments); 10 | this.knownLanguage = knownLanguage; 11 | } 12 | 13 | var dev = new Developer("Mario", "Rossi", "JavaScript"); 14 | console.log(dev.knownLanguage); //result: undefined -------------------------------------------------------------------------------- /Chapter 4/Code14.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | Object.seal(this); 6 | } 7 | 8 | 9 | var person = new Person("John", "Smith"); 10 | 11 | console.log(delete person.name); //result: false 12 | console.log(person.name); //result: "John" 13 | 14 | 15 | if (!Object.isSealed(person)) { 16 | delete person.name; 17 | } -------------------------------------------------------------------------------- /Chapter 4/Code15.txt: -------------------------------------------------------------------------------- 1 | var person = new Person("John", "Smith"); 2 | 3 | Object.freeze(person); 4 | 5 | person.age = 32; 6 | console.log(person.age); //result: undefined 7 | 8 | person.name = "Mario"; 9 | console.log(person.name); //result: "John" 10 | 11 | delete person.name; //result: false 12 | console.log(person.name); //result: "John" 13 | 14 | Object.defineProperty( 15 | person, 16 | "name", 17 | { get: function() { return "Mario"; } 18 | }); 19 | //result: exception 20 | 21 | if (!Object.isFrozen(person)) { 22 | person.name = "Mario"; 23 | } -------------------------------------------------------------------------------- /Chapter 4/Code16.txt: -------------------------------------------------------------------------------- 1 | function Developer(name, surname, knownLanguage) { 2 | Person.apply(this, arguments); 3 | this.knownLanguage = knownLanguage; 4 | } 5 | 6 | function Student(name, surname, subjectOfStudy) { 7 | Person.apply(this, arguments); 8 | this.subjectOfStudy = subjectOfStudy; 9 | } 10 | 11 | function DevStudent(name, surname, knownLanguage, subjectOfStudy) { 12 | Developer.call(this, name, surname, knownLanguage); 13 | Student.call(this, name, surname, subjectOfStudy); 14 | } 15 | 16 | var johnSmith = new DevStudent("John", "Smith", "C#", "JavaScript"); 17 | 18 | console.log(johnSmith.knownLanguage); //result: C# 19 | console.log(johnSmith.subjectOfStudy); //result: JavaScript 20 | -------------------------------------------------------------------------------- /Chapter 4/Code17.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | } 5 | 6 | var myMixin = { 7 | getFullName: function() { 8 | return this.name + " " + this.surname; 9 | } 10 | }; 11 | 12 | function augment(destination, source) { 13 | for (var methodName in source) { 14 | if (source.hasOwnProperty(methodName)) { 15 | destination[methodName] = source[methodName]; 16 | } 17 | } 18 | return destination; 19 | } 20 | 21 | augment(Person.prototype, myMixin); 22 | 23 | var johnSmith = new Person("John", "Smith"); 24 | 25 | console.log(johnSmith.getFullName()); //result: "John Smith" 26 | -------------------------------------------------------------------------------- /Chapter 4/Code18.txt: -------------------------------------------------------------------------------- 1 | function augment(destination, source, ...methodNames) { 2 | if (methodNames) { 3 | for (var methodName of methodNames) { 4 | if (source.hasOwnProperty(methodName)) { 5 | destination[methodName] = source[methodName]; 6 | } 7 | } 8 | } else { 9 | for (var methodName in source) { 10 | if (source.hasOwnProperty(methodName)) { 11 | destination[methodName] = source[methodName]; 12 | } 13 | } 14 | } 15 | 16 | return destination; 17 | } -------------------------------------------------------------------------------- /Chapter 4/Code19.txt: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | } 7 | 8 | var myMixin = { 9 | getFullName: function() { 10 | return this.name + " " + this.surname; 11 | } 12 | }; 13 | 14 | 15 | augment(Person.prototype, myMixin); 16 | 17 | var johnSmith = new Person("John", "Smith"); 18 | 19 | console.log(johnSmith.getFullName()); //result: "John Smith" 20 | -------------------------------------------------------------------------------- /Chapter 4/Code20.txt: -------------------------------------------------------------------------------- 1 | function mixNamingWith(superclass) { 2 | return class extends superclass { 3 | getFullName() { 4 | return this.name + " " + this.surname; 5 | } 6 | } 7 | } 8 | 9 | class ExtendedPerson extends mixNamingWith(Person) { } 10 | 11 | var johnSmith = new ExtendedPerson("John", "Smith"); 12 | 13 | console.log(johnSmith .getFullName()); //result: "John Smith" 14 | -------------------------------------------------------------------------------- /Chapter 5/Code01.txt: -------------------------------------------------------------------------------- 1 | function square(n) { 2 | return n * n; 3 | } 4 | 5 | console.log(square(3)); //result: 9 6 | 7 | console.log(square("3")); //result: 9 8 | 9 | console.log(square("three")); //result: NaN 10 | console.log(square(true)); //result: 1 11 | console.log(square({a: 2})); //result: NaN -------------------------------------------------------------------------------- /Chapter 5/Code02.txt: -------------------------------------------------------------------------------- 1 | function square(n) { 2 | var result; 3 | 4 | if (typeof n === "number") { 5 | result = n*n; 6 | } else { 7 | throw new Error("Wrong data type!") 8 | } 9 | 10 | return result; 11 | } 12 | 13 | console.log(square("three")); //result: Error: Wrong data type! 14 | -------------------------------------------------------------------------------- /Chapter 5/Code03.txt: -------------------------------------------------------------------------------- 1 | function SoftwareHouse() { 2 | this.employees = []; 3 | 4 | } 5 | 6 | SoftwareHouse.prototype.hire = function(dev) { 7 | this.employees.push(dev); 8 | }; 9 | 10 | /* 11 | class SoftwareHouse { 12 | constructor() { 13 | this.employees = []; 14 | } 15 | 16 | hire(dev) { 17 | this.employees.push(dev); 18 | } 19 | } 20 | */ 21 | 22 | var johnSmith = {name: "John", surname: "Smith"}; 23 | var lassie = {name: "Lassie", breed: "Collie"}; 24 | var table = {type: "round", legsNumber: 1}; 25 | 26 | var swHouse = new SoftwareHouse(); 27 | 28 | swHouse.hire(johnSmith); 29 | swHouse.hire(lassie); 30 | swHouse.hire(table); 31 | 32 | console.log(swHouse.employees.length); //result: 3 -------------------------------------------------------------------------------- /Chapter 5/Code04.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname; 4 | } 5 | 6 | var johnSmith = new Person("John", "Smith"); 7 | 8 | console.log(johnSmith instanceof Person); //result: true 9 | 10 | class SoftwareHouse { 11 | constructor() { 12 | this.employees = []; 13 | } 14 | 15 | hire(dev) { 16 | if (dev instanceof Person) { 17 | this.employees.push(dev); 18 | } else { 19 | throw new Error("This software house hires only persons!"); 20 | } 21 | } 22 | } 23 | 24 | var johnSmith = new Person("John", "Smith"); 25 | var lassie = {name: "Lassie", breed: "Collie"}; 26 | var table = {type: "round", legsNumber: 1}; 27 | 28 | var swHouse = new SoftwareHouse(); 29 | 30 | swHouse.hire(johnSmith); 31 | swHouse.hire(lassie); //result: Error 32 | swHouse.hire(table); //result: Error 33 | -------------------------------------------------------------------------------- /Chapter 5/Code05.txt: -------------------------------------------------------------------------------- 1 | class SoftwareHouse { 2 | constructor() { 3 | this.employees = []; 4 | } 5 | 6 | hire(dev) { 7 | if (dev instanceof Person) { 8 | this.employees.push(dev); 9 | } else { 10 | throw new Error("This software house hires only persons!"); 11 | } 12 | } 13 | 14 | createSoftware() { 15 | var newSoftware = []; 16 | var employee; 17 | var module; 18 | 19 | for(var i = 0; i < this.employees.length; i++) { 20 | employee = this.employees[i]; 21 | module = employee.writeCode(); 22 | newSoftware.push(module); 23 | } 24 | 25 | return newSoftware; 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter 5/Code06.txt: -------------------------------------------------------------------------------- 1 | function Developer(name, surname, knownLanguage) { 2 | 3 | Person.apply(this, arguments); 4 | 5 | this.knownLanguage = knownLanguage; 6 | 7 | } 8 | 9 | 10 | Developer.prototype = Object.create(Person.prototype); 11 | Developer.prototype.constructor = Developer; 12 | 13 | function Student(name, surname, subjectOfStudy) { 14 | 15 | Person.apply(this, arguments); 16 | 17 | this.subjectOfStudy = subjectOfStudy; 18 | 19 | } 20 | 21 | Student.prototype = Object.create(Person.prototype); 22 | Student.prototype.constructor = Student; 23 | 24 | 25 | 26 | function DevStudent(name, surname, knownLanguage, subjectOfStudy) { 27 | 28 | Developer.call(this, name, surname, knownLanguage); 29 | 30 | Student.call(this, name, surname, subjectOfStudy); 31 | 32 | } 33 | 34 | DevStudent.prototype.writeCode = function() { 35 | console.log("writing code..."); 36 | return {module: "..."}; 37 | }; 38 | 39 | 40 | var johnSmith = new DevStudent("John", "Smith", "C#", "JavaScript"); 41 | 42 | console.log(johnSmith instanceof Student); //result: false 43 | console.log(johnSmith instanceof Developer); //result: false 44 | console.log(johnSmith instanceof Person); //result: false 45 | -------------------------------------------------------------------------------- /Chapter 5/Code07.txt: -------------------------------------------------------------------------------- 1 | class SoftwareHouse { 2 | constructor() { 3 | this.employees = []; 4 | } 5 | 6 | hire(dev) { 7 | if (dev && dev["writeCode"] && dev["writeCode"] instanceof Function) { 8 | this.employees.push(dev); 9 | } else { 10 | throw new Error("The argument do not implements writeCode method") 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter 5/Code08.txt: -------------------------------------------------------------------------------- 1 | var SoftwareHouse = (function() { 2 | 3 | function implement(obj, method) { 4 | return (obj && obj[method] && obj[method] instanceof Function); 5 | } 6 | 7 | return class { 8 | constructor() { 9 | this.employees = []; 10 | } 11 | 12 | hire(dev) { 13 | if (implement(dev, "writeCode")) { 14 | this.employees.push(dev); 15 | } else { 16 | throw new Error("The argument does not implement writeCode method") 17 | } 18 | } 19 | }; 20 | })(); -------------------------------------------------------------------------------- /Chapter 5/Code09.txt: -------------------------------------------------------------------------------- 1 | var SoftwareHouse = (function() { 2 | 3 | function implementsMethod(obj, method) { 4 | return !!(obj && obj[method] && obj[method] instanceof Function); 5 | } 6 | 7 | function implementsProperty(obj, property) { 8 | return !!(obj && obj[property] && !(obj[property] instanceof Function)) 9 | } 10 | 11 | return class { 12 | constructor() { 13 | this.employees = []; 14 | } 15 | 16 | hire(dev) { 17 | if (implementsMethod(dev, "writeCode") && implementsProperty(dev, "name")) { 18 | this.employees.push(dev); 19 | } else { 20 | throw new Error("The argument is not compatible with the required interface") 21 | } 22 | } 23 | }; 24 | })();); -------------------------------------------------------------------------------- /Chapter 5/Code10.txt: -------------------------------------------------------------------------------- 1 | Object.prototype.implementsMethod = function(method) { 2 | return !!(this[method] && this[method] instanceof Function) 3 | }; 4 | 5 | Object.prototype.implementsProperty = function(property) { 6 | return !!(this[property] && !(this[property] instanceof Function)) 7 | }; 8 | 9 | var johnSmith = {name: "John", surname: "Smith", writeCode: function() {...}}; 10 | 11 | johnSmith.implementsMethod("name"); //result: true 12 | johnSmith.implementsMethod("writeCode"); //result: true 13 | johnSmith.implementsMethod("writePoems"); //result: false 14 | 15 | 16 | class SoftwareHouse { 17 | constructor() { 18 | this.employees = []; 19 | } 20 | 21 | hire(dev) { 22 | if (dev.implementsMethod("writeCode") && dev.implementsProperty("name")) { 23 | this.employees.push(dev); 24 | } else { 25 | throw new Error("The argument is not compatible with the required interface") 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter 5/Code11.txt: -------------------------------------------------------------------------------- 1 | class Interface { 2 | constructor(name, methods=[], properties=[]) { 3 | this.name = name; 4 | this.methods = []; 5 | this.properties = []; 6 | 7 | for (let i = 0, len = methods.length; i < len; i++) { 8 | if (typeof methods[i] !== 'string') { 9 | throw new Error("Interface constructor expects method names to be passed in as a string."); 10 | } 11 | this.methods.push(methods[i]); 12 | } 13 | 14 | for (let i = 0, len = properties.length; i < len; i++) { 15 | if (typeof properties[i] !== 'string') { 16 | throw new Error("Interface constructor expects property names to be passed in as a string."); 17 | } 18 | this.properties.push(properties[i]); 19 | } 20 | } 21 | 22 | isImplementedBy(obj) { 23 | var methodsLen = this.methods.length; 24 | var propertiesLen = this.properties.length; 25 | var currentMember; 26 | 27 | if (obj) { 28 | //check methods 29 | for (let i = 0; i < methodsLen; i++) { 30 | currentMember = this.methods[i]; 31 | if (!obj[currentMember] || typeof obj[currentMember] !== "function") { 32 | throw new Error("The object does not implement the interface " + this.name + ". Method " + currentMember + " not found."); 33 | } 34 | } 35 | 36 | //check properties 37 | for (let i = 0; i < propertiesLen; i++) { 38 | currentMember = this.properties[i]; 39 | if (!obj[currentMember] || typeof obj[currentMember] === "function") { 40 | throw new Error("The object does not implement the interface " + this.name + ". Property " + currentMember + " not found."); 41 | } 42 | } 43 | } else { 44 | throw new Error("No object to check!"); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Chapter 5/Code12.txt: -------------------------------------------------------------------------------- 1 | function Interface(name, methods, properties) { 2 | "use strict"; 3 | 4 | methods = methods || []; 5 | properties = properties || []; 6 | 7 | this.name = name; 8 | this.methods = []; 9 | this.properties = []; 10 | 11 | for (let i = 0, len = methods.length; i < len; i++) { 12 | if (typeof methods[i] !== 'string') { 13 | throw new Error("Interface constructor expects method names to be passed in as a string."); 14 | } 15 | this.methods.push(methods[i]); 16 | } 17 | 18 | for (let i = 0, len = properties.length; i < len; i++) { 19 | if (typeof properties[i] !== 'string') { 20 | throw new Error("Interface constructor expects property names to be passed in as a string."); 21 | } 22 | this.properties.push(properties[i]); 23 | } 24 | } 25 | 26 | 27 | Interface.prototype.isImplementedBy = function(obj) { 28 | "use strict"; 29 | var methodsLen = this.methods.length; 30 | var propertiesLen = this.properties.length; 31 | var currentMember; 32 | 33 | if (obj) { 34 | //check methods 35 | for (let i = 0; i < methodsLen; i++) { 36 | currentMember = this.methods[i]; 37 | if (!obj[currentMember] || typeof obj[currentMember] !== "function") { 38 | throw new Error("The object does not implement the interface " + this.name + ". Method " + currentMember + " not found."); 39 | } 40 | } 41 | 42 | //check properties 43 | for (let i = 0; i < propertiesLen; i++) { 44 | currentMember = this.properties[i]; 45 | if (!obj[currentMember] || typeof obj[currentMember] === "function") { 46 | throw new Error("The object does not implement the interface " + this.name + ". Property " + currentMember + " not found."); 47 | } 48 | } 49 | } else { 50 | throw new Error("No object to check!"); 51 | } 52 | }; -------------------------------------------------------------------------------- /Chapter 5/Code13.txt: -------------------------------------------------------------------------------- 1 | function Interface(name, methods, properties) { 2 | "use strict"; 3 | 4 | methods = methods || []; 5 | properties = properties || []; 6 | 7 | this.name = name; 8 | this.methods = []; 9 | this.properties = []; 10 | 11 | for (let i = 0, len = methods.length; i < len; i++) { 12 | if (typeof methods[i] !== 'string') { 13 | throw new Error("Interface constructor expects method names to be passed in as a string."); 14 | } 15 | this.methods.push(methods[i]); 16 | } 17 | 18 | for (let i = 0, len = properties.length; i < len; i++) { 19 | if (typeof properties[i] !== 'string') { 20 | throw new Error("Interface constructor expects property names to be passed in as a string."); 21 | } 22 | this.properties.push(properties[i]); 23 | } 24 | } 25 | 26 | 27 | Interface.prototype.isImplementedBy = function(obj) { 28 | "use strict"; 29 | var methodsLen = this.methods.length; 30 | var propertiesLen = this.properties.length; 31 | var currentMember; 32 | 33 | if (obj) { 34 | //check methods 35 | for (let i = 0; i < methodsLen; i++) { 36 | currentMember = this.methods[i]; 37 | if (!obj[currentMember] || typeof obj[currentMember] !== "function") { 38 | throw new Error("The object does not implement the interface " + this.name + ". Method " + currentMember + " not found."); 39 | } 40 | } 41 | 42 | //check properties 43 | for (let i = 0; i < propertiesLen; i++) { 44 | currentMember = this.properties[i]; 45 | if (!obj[currentMember] || typeof obj[currentMember] === "function") { 46 | throw new Error("The object does not implement the interface " + this.name + ". Property " + currentMember + " not found."); 47 | } 48 | } 49 | } else { 50 | throw new Error("No object to check!"); 51 | } 52 | }; 53 | 54 | var IHireable = new Interface("IHireable", ["writeCode"], ["name"]); 55 | 56 | class SoftwareHouse { 57 | constructor() { 58 | this.employees = []; 59 | } 60 | 61 | hire(dev) { 62 | IHireable.isImplementedBy(dev); 63 | this.employees.push(dev); 64 | } 65 | } -------------------------------------------------------------------------------- /Chapter 5/Code14.txt: -------------------------------------------------------------------------------- 1 | var ITeamLeadership = new Interface("ITeamLeadership", ["delegateTo", "motivate"], ["team"]); 2 | 3 | class SoftwareHouse { 4 | constructor() { 5 | this.employees = []; 6 | } 7 | 8 | hire(dev) { 9 | IHireable.isImplementedBy(dev); 10 | ITeamLeadership.isImplementedBy(dev); 11 | this.employees.push(dev); 12 | } 13 | } 14 | 15 | var johnSmith = { 16 | name: "John", 17 | surname: "Smith", 18 | writeCode: function() {...}, 19 | delegateTo: function() {...}, 20 | motivate: function() {...}, 21 | team: []}; 22 | 23 | var swHouse = new SoftwareHouse(); 24 | 25 | swHouse.hire(johnSmith); 26 | 27 | console.log(swHouse.employees.indexOf(johnSmith)); //result: 0 28 | -------------------------------------------------------------------------------- /Chapter 5/Code15.txt: -------------------------------------------------------------------------------- 1 | class Developer { 2 | constructor(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | } 7 | 8 | class Salesman { 9 | constructor(name, surname) { 10 | this.firstName = name; 11 | this.secondName = surname; 12 | } 13 | } 14 | 15 | class BusinessAnalyst { 16 | constructor(fullName) { 17 | this.fullName = fullNname; 18 | } 19 | } 20 | 21 | 22 | class SoftwareHouse { 23 | constructor() { 24 | this.employees = []; 25 | } 26 | 27 | listEmployees() { 28 | var employeesLen = this.employees.length; 29 | var currentEmployee; 30 | 31 | for(var i = 0; i < employeesLen; i++) { 32 | currentEmployee = this.employees[i]; 33 | 34 | if (currentEmployee instanceof Developer) { 35 | console.log(currentEmployee.name + " " + currentEmployee.surname); 36 | } else if (currentEmployee instanceof Salesman) { 37 | console.log(currentEmployee.firstName + " " + currentEmployee.secondName); 38 | } else if (currentEmployee instanceof BusinessAnalyst) { 39 | console.log(currentEmployee.fullName); 40 | } 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Chapter 6/Code01.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | return "This is a person"; 6 | } 7 | 8 | var johnSmith = new Person("John", "Smith"); 9 | 10 | console.log(johnSmith.name); //John 11 | console.log(johnSmith.surname); //Smith -------------------------------------------------------------------------------- /Chapter 6/Code02.txt: -------------------------------------------------------------------------------- 1 | function Person(name, surname) { 2 | this.name = name; 3 | this.surname = surname; 4 | 5 | return { firstName: name, secondName: surname }; 6 | } 7 | 8 | var johnSmith = new Person("John", "Smith"); 9 | 10 | console.log(johnSmith.name); //undefined 11 | console.log(johnSmith.surname); //undefined 12 | console.log(johnSmith.firstName); //John 13 | console.log(johnSmith.secondName); //Smith -------------------------------------------------------------------------------- /Chapter 6/Code03.txt: -------------------------------------------------------------------------------- 1 | var IdGenerator = (function() { 2 | var instance; 3 | var counter = 0; 4 | 5 | var Constructor = function() { 6 | if (!instance) { 7 | instance = this; 8 | } 9 | 10 | return instance; 11 | }; 12 | 13 | Constructor.prototype.newId = function() { 14 | return ++counter; 15 | }; 16 | 17 | return Constructor; 18 | })(); 19 | 20 | 21 | var g = new IdGenerator(); 22 | 23 | console.log(g.newId()); //result: 1 24 | console.log(g.newId()); //result: 2 25 | 26 | var g1 = new IdGenerator(); 27 | 28 | console.log(g1.newId()); //result: 3 29 | -------------------------------------------------------------------------------- /Chapter 6/Code04.txt: -------------------------------------------------------------------------------- 1 | var IdGenerator = (function(){ 2 | var instance; 3 | var counter = 0; 4 | 5 | return class { 6 | constructor() { 7 | if (!instance) { 8 | instance = this; 9 | } 10 | 11 | return instance; 12 | } 13 | 14 | newId() { 15 | return ++counter; 16 | } 17 | }; 18 | })(); 19 | -------------------------------------------------------------------------------- /Chapter 6/Code05.txt: -------------------------------------------------------------------------------- 1 | class Developer { 2 | constructor(skills, benefits) { 3 | this.skills = ["programming"].concat(skills); 4 | this.salary = 40000; 5 | this.benefits = ["computer"].concat(benefits); 6 | } 7 | } 8 | 9 | class SoftwareHouse { 10 | constructor() { 11 | this.employees = []; 12 | } 13 | 14 | hireDeveloper() { 15 | var dev = new Developer(["JavaScript"], ["smartphone"]); 16 | this.employees.push(dev); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter 6/Code06.txt: -------------------------------------------------------------------------------- 1 | class Salesman { 2 | constructor(skills, benefits) { 3 | this.skills = ["selling"].concat(skills); 4 | this.salary = 50000; 5 | this.benefits = ["computer"].concat(benefits); 6 | } 7 | } 8 | 9 | class BusinessAnalyst { 10 | constructor(skills, benefits) { 11 | this.skills = ["analyzing"].concat(skills); 12 | this.salary = 60000; 13 | this.benefits = ["computer"].concat(benefits); 14 | } 15 | } 16 | 17 | class SoftwareHouse { 18 | constructor() { 19 | this.employees = []; 20 | } 21 | 22 | hireDeveloper() { 23 | var dev = new Developer(["JavaScript"], ["smartphone"]); 24 | this.employees.push(dev); 25 | } 26 | 27 | hireSalesman() { 28 | var sm = new Salesman(["communication"], ["smartphone", "car"]); 29 | this.employees.push(sm); 30 | } 31 | 32 | hireBusinessAnalyst() { 33 | var ba = new BusinessAnalyst(["communication", "writing"], ["smartphone", "tablet"]); 34 | this.employees.push(ba); 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter 6/Code07.txt: -------------------------------------------------------------------------------- 1 | class Developer { 2 | constructor(skills, benefits) { 3 | this.skills = ["programming"].concat(skills); 4 | this.salary = 40000; 5 | this.benefits = ["computer"].concat(benefits); 6 | } 7 | } 8 | 9 | class RecruitmentAgency { 10 | 11 | getStaffMember(role, skills, benefits) { 12 | var member; 13 | 14 | switch(role .toLowerCase()) { 15 | case "dev": 16 | member = new Developer(skills, benefits); 17 | break; 18 | case "sale": 19 | member = new Salesman(skills, benefits); 20 | break; 21 | case "ba": 22 | member = new BusinessAnalyst(skills, benefits); 23 | break; 24 | default: 25 | throw new Error("Unable to hire people for the role " + role) 26 | } 27 | 28 | return member; 29 | } 30 | } 31 | 32 | var agency = new RecruitmentAgency(); 33 | 34 | var newDevStaffMember = agency.getStaffMember("dev", ["C++", "C#"], ["tablet"]); -------------------------------------------------------------------------------- /Chapter 6/Code08.txt: -------------------------------------------------------------------------------- 1 | class Developer { 2 | constructor(skills, benefits) { 3 | this.skills = ["programming"].concat(skills); 4 | this.salary = 40000; 5 | this.benefits = ["computer"].concat(benefits); 6 | } 7 | } 8 | 9 | class BusinessAnalyst { 10 | constructor(skills, benefits) { 11 | this.skills = ["analyzing"].concat(skills); 12 | this.salary = 60000; 13 | this.benefits = ["computer"].concat(benefits); 14 | } 15 | } 16 | 17 | class Salesman { 18 | constructor(skills, benefits) { 19 | this.skills = ["selling"].concat(skills); 20 | this.salary = 50000; 21 | this.benefits = ["computer"].concat(benefits); 22 | } 23 | } 24 | 25 | class RecruitmentAgency { 26 | constructor() { 27 | this.objConstructors = {}; 28 | } 29 | 30 | register(role, constructor) { 31 | this.objConstructors[role] = constructor; 32 | } 33 | 34 | getStaffMember(role, skills, benefits) { 35 | var objConstructor = this.objConstructors[role]; 36 | var member; 37 | 38 | if (objConstructor) member = new objConstructor(skills, benefits); 39 | 40 | return member; 41 | } 42 | } 43 | 44 | var agency = new RecruitmentAgency(); 45 | 46 | agency.register("dev", Developer); 47 | agency.register("ba", BusinessAnalyst); 48 | agency.register("sale", Salesman); 49 | -------------------------------------------------------------------------------- /Chapter 6/Code09.txt: -------------------------------------------------------------------------------- 1 | class Developer { 2 | constructor(skills, benefits) { 3 | this.skills = ["programming"].concat(skills); 4 | this.salary = 40000; 5 | this.benefits = ["computer"].concat(benefits); 6 | } 7 | } 8 | 9 | class Salesman { 10 | constructor(skills, benefits) { 11 | this.skills = ["selling"].concat(skills); 12 | this.salary = 50000; 13 | this.benefits = ["computer"].concat(benefits); 14 | } 15 | } 16 | 17 | class BusinessAnalyst { 18 | constructor(skills, benefits) { 19 | this.skills = ["analyzing"].concat(skills); 20 | this.salary = 60000; 21 | this.benefits = ["computer"].concat(benefits); 22 | } 23 | } 24 | 25 | 26 | class DevAgency { 27 | getStaffMember(skills, benefits) { 28 | return new Developer(skills, benefits); 29 | } 30 | } 31 | 32 | class SalesAgency { 33 | getStaffMember(skills, benefits) { 34 | return new Salesman(skills, benefits); 35 | } 36 | } 37 | 38 | class BusinessAnalystAgency { 39 | getStaffMember(skills, benefits) { 40 | return new BusinessAnalyst(skills, benefits); 41 | } 42 | } 43 | 44 | class RecruitmentAgencyAbstractFactory { 45 | 46 | constructor() { 47 | this.agencyFactories = {}; 48 | } 49 | 50 | register(area, agencyFactory) { 51 | this.agencyFactories[area] = agencyFactory; 52 | } 53 | 54 | getAgency (area) { 55 | return new this.agencyFactories[area]; 56 | } 57 | } 58 | 59 | var agencyFinder = new RecruitmentAgencyAbstractFactory(); 60 | 61 | agencyFinder.register("dev", DevAgency); 62 | agencyFinder.register("sales", SalesAgency); 63 | agencyFinder.register("ba", BusinessAnalystAgency); 64 | 65 | var devAgency = agencyFinder.getAgency("dev"); 66 | var newDevMember = devAgency.getStaffMember(["JavaScript"], ["phone"]); 67 | -------------------------------------------------------------------------------- /Chapter 6/Code10.txt: -------------------------------------------------------------------------------- 1 | var ObjectPool = (function(){ 2 | var instance; 3 | var objConstructor; 4 | var objPool = []; 5 | 6 | return class { 7 | constructor(objConstr) { 8 | if (!instance) { 9 | objConstructor = objConstr; 10 | instance = this; 11 | } 12 | 13 | return instance; 14 | } 15 | 16 | get() { 17 | var obj; 18 | 19 | if (objPool.length == 0) { 20 | obj = new objConstructor(); 21 | } else { 22 | obj = objPool.pop(); 23 | } 24 | 25 | return obj; 26 | } 27 | 28 | recycle(obj) { 29 | objPool.push(obj); 30 | } 31 | }; 32 | })(); 33 | 34 | 35 | class Trainer { 36 | explain() {} 37 | show() {} 38 | exercise() {} 39 | } 40 | 41 | 42 | var trainerPool = new ObjectPool(Trainer); 43 | 44 | var trainer1 = trainerPool.get(); 45 | var trainer2 = trainerPool.get(); 46 | 47 | trainerPool.recycle(trainer1); 48 | 49 | var trainer3 = trainerPool.get(); 50 | 51 | console.log(trainer1 == trainer2); //false 52 | console.log(trainer1 == trainer3); //true 53 | -------------------------------------------------------------------------------- /Chapter 7/Code01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 54 | 55 | 56 | 57 |
58 |
59 |
60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /Chapter 7/Code02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 88 | 89 | 90 | 91 |
92 |
93 |
94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /Chapter 7/Code03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 88 | 89 | 90 | 91 |
92 |
93 |
94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /Chapter 7/Code04.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 102 | 103 | 104 | 105 |
106 |
107 |
108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 7/Code05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 95 | 96 | 97 | 98 |
99 |
100 |
101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /Chapter 7/Code06.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 100 | 101 | 102 | 103 |
104 |
105 |
106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /Chapter 7/Code07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 96 | 97 | 98 | 99 |
100 |
101 |
102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /Chapter 8/Code01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 27 | 28 | 29 | 30 |
31 |
32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Chapter 8/Code02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 29 | 30 | 31 | 32 |
33 |
34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter 8/Code03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 42 | 43 | 44 | 45 |
46 |
47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter 8/Code04.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 46 | 47 | 48 | 49 |
50 |
51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Chapter 8/Code05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | 37 | 38 | 39 |
40 |
41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Chapter 8/Code06.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 53 | 54 | 55 | 56 |
57 |
58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Chapter 8/Code07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 40 | 41 | 42 | 43 |
44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Chapter 8/Code08.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 44 | 45 | 46 | 47 |
48 |
49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Chapter 9/Code01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter 9/Code02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 9/Code03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter 9/Code04.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter 9/Code05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter 9/Code06.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Chapter 9/Code07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Chapter 9/Code08.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Chapter 9/Code09.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Chapter 9/Code10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Chapter 9/Code11.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Chapter 9/Code12.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Mastering-JavaScript-Object-Oriented-Programming 5 | [Mastering JavaScript Object-Oriented Programming](https://www.packtpub.com/web-development/mastering-javascript-object-oriented-programming?utm_source=GitHub&utm_medium=repository&utm_campaign=9781785889103) by [Packt Publishing](https://www.packtpub.com/) 6 | 7 | 8 | Most of the code provided in this book is not bound to a specific JavaScript runtime environment, so it could run in any JavaScript environment. However some examples are specific for the Web browser environment, so the console of Web Inspector such as Chrome Developer tools or Firebug is needed. 9 | 10 | Chapter 1 has no codes. 11 | 12 | For more information refer to the following books: 13 | * [Object-Oriented JavaScript](https://www.packtpub.com/web-development/object-oriented-javascript?utm_source=GitHub&utm_medium=repository&utm_campaign=9781847194145) 14 | * [Mastering JavaScript Design Patterns](https://www.packtpub.com/application-development/mastering-javascript-design-patterns?utm_source=GitHub&utm_medium=repository&utm_campaign=9781783987986) 15 | * [Learning Object-Oriented Programming](https://www.packtpub.com/application-development/learning-object-oriented-programming?utm_source=GitHub&utm_medium=repository&utm_campaign=9781785289637) 16 | ### Download a free PDF 17 | 18 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
19 |

https://packt.link/free-ebook/9781785889103

--------------------------------------------------------------------------------