GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — js_designpatterns/factory.js at master · m1oto/js_designpatterns Expand file tree/ Copy pathfactory.jsMore file actions 86 lines (79 loc) · 2.16 KB / Copy pathfactory.jsFile metadata and controls86 lines (79 loc) · 2.16 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 // Credit to Yehuda Katz for `fromPrototype` function // http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ var fromPrototype = function(prototype, object) { var newObject = Object.create(prototype); for (var prop in object) { if (object.hasOwnProperty(prop)) { newObject[prop] = object[prop]; } } return newObject; }; // Define our `DeviceFactory` base object var DeviceFactory = { screen: function() { return 'retina'; }, battery: function() { return 'lithium ion battery'; }, keypad: function() { return 'keyboard'; }, processor: function() { return 'Intel Core-i5'; } }; // Extend `DeviceFactory` with other implementations DeviceFactory.makeLaptop = function() { return fromPrototype(DeviceFactory, { screen: function() { return 'retina 13 inches'; }, battery: function() { return 'lithium ion 9 hours battery'; }, keypad: function() { return 'backlit keyboard'; }, processor: function() { return 'Intel Core-i5' } }); }; DeviceFactory.makeSmartPhone = function() { return fromPrototype(DeviceFactory, { screen: function() { return 'retina 5 inches'; }, battery: function() { return 'lithium ion 15 hours'; }, keypad: function() { return 'touchscreen keypad'; }, processor: function() { return 'ARMv8' } }); }; DeviceFactory.makeTablet = function() { return fromPrototype(DeviceFactory, { screen: function() { return 'retina 9 inches'; }, battery: function() { return 'lithium ion 15 hours'; }, keypad: function() { return 'touchscreen keypad'; }, processor: function() { return 'ARMv8' } }); }; var appleMacbookPro = DeviceFactory.makeLaptop(); console.log(appleMacbookPro.screen()); // returns 'retina 13 inches'; var iPhoneSomeS = DeviceFactory.makeSmartPhone(); var iPadSomeSS = DeviceFactory.makeTablet(); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |