|
| 1 | +//*******************************************************// |
| 2 | +// Object Creation |
| 3 | +//*******************************************************// |
| 4 | + |
| 5 | +//********************** Snippet 1 **********************// |
| 6 | + |
| 7 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 8 | +// Each of the following options will create a new empty object: |
| 9 | + |
| 10 | +const newObject = {}; |
| 11 | + |
| 12 | +// or |
| 13 | +const newObject = Object.create(Object.prototype); |
| 14 | + |
| 15 | +// or |
| 16 | +const newObject = new Object(); |
| 17 | + |
| 18 | +//********************** Snippet 2 **********************// |
| 19 | + |
| 20 | +// ECMAScript 3 compatible approaches |
| 21 | + |
| 22 | +// 1. Dot syntax |
| 23 | + |
| 24 | +// Set properties |
| 25 | +newObject.someKey = 'Hello World'; |
| 26 | + |
| 27 | +// Get properties |
| 28 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 29 | +const value = newObject.someKey; |
| 30 | + |
| 31 | +// 2. Square bracket syntax |
| 32 | + |
| 33 | +// Set properties |
| 34 | +newObject['Some Key'] = 'Hello World'; |
| 35 | + |
| 36 | +// Get properties |
| 37 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 38 | +const value = newObject['Some Key']; |
| 39 | + |
| 40 | +// ECMAScript 5 only compatible approaches |
| 41 | +// For more information see: http://kangax.github.com/es5-compat-table/ |
| 42 | + |
| 43 | +// 3. Object.defineProperty |
| 44 | + |
| 45 | +// Set properties |
| 46 | +Object.defineProperty(newObject, 'someKey', { |
| 47 | + value: "for more control of the property's behavior", |
| 48 | + writable: true, |
| 49 | + enumerable: true, |
| 50 | + configurable: true, |
| 51 | +}); |
| 52 | + |
| 53 | +// If the above feels a little difficult to read, a short-hand could |
| 54 | +// be written as follows: |
| 55 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 56 | +// [ES2015+] We used new arrow function expression syntax |
| 57 | +const defineProp = (obj, key, value) => { |
| 58 | + const config = { |
| 59 | + value: value, |
| 60 | + writable: true, |
| 61 | + enumerable: true, |
| 62 | + configurable: true, |
| 63 | + }; |
| 64 | + Object.defineProperty(obj, key, config); |
| 65 | +}; |
| 66 | + |
| 67 | +// To use, we then create a new empty "person" object |
| 68 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 69 | +const person = Object.create(Object.prototype); |
| 70 | + |
| 71 | +// Populate the object with properties |
| 72 | +defineProp(person, 'car', 'Delorean'); |
| 73 | +defineProp(person, 'dateOfBirth', '1981'); |
| 74 | +defineProp(person, 'hasBeard', false); |
| 75 | + |
| 76 | +console.log(person); |
| 77 | +// Outputs: Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false} |
| 78 | + |
| 79 | +// 4. Object.defineProperties |
| 80 | + |
| 81 | +// Set properties |
| 82 | +Object.defineProperties(newObject, { |
| 83 | + someKey: { |
| 84 | + value: 'Hello World', |
| 85 | + writable: true, |
| 86 | + }, |
| 87 | + |
| 88 | + anotherKey: { |
| 89 | + value: 'Foo bar', |
| 90 | + writable: false, |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +// Getting properties for 3. and 4. can be done using any of the |
| 95 | +// options in 1. and 2. |
| 96 | + |
| 97 | +//********************** Snippet 3 **********************// |
| 98 | + |
| 99 | +// Usage: |
| 100 | + |
| 101 | +// Create a race car driver that inherits from the person object |
| 102 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 103 | +const driver = Object.create(person); |
| 104 | + |
| 105 | +// Set some properties for the driver |
| 106 | +defineProp(driver, 'topSpeed', '100mph'); |
| 107 | + |
| 108 | +// Get an inherited property (1981) |
| 109 | +console.log(driver.dateOfBirth); |
| 110 | + |
| 111 | +// Get the property we set (100mph) |
| 112 | +console.log(driver.topSpeed); |
| 113 | + |
| 114 | +//*******************************************************// |
| 115 | +// Basic Constructors |
| 116 | +//*******************************************************// |
| 117 | + |
| 118 | +//********************** Snippet 1 **********************// |
| 119 | + |
| 120 | +// [ES2015+] Below we used new class declaration, using keyword class |
| 121 | +// [ES2015+] We used new constructor method and method declaration |
| 122 | +// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance |
| 123 | +// [ES2015+] We used new template literals for string interpolation |
| 124 | +class Car { |
| 125 | + constructor(model, year, miles) { |
| 126 | + this.model = model; |
| 127 | + this.year = year; |
| 128 | + this.miles = miles; |
| 129 | + } |
| 130 | + |
| 131 | + toString() { |
| 132 | + return `${this.model} has done ${this.miles} miles`; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Usage: |
| 137 | + |
| 138 | +// We can create new instances of the car |
| 139 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 140 | +const civic = new Car('Honda Civic', 2009, 20000); |
| 141 | +const mondeo = new Car('Ford Mondeo', 2010, 5000); |
| 142 | + |
| 143 | +// and then open our browser console to view the |
| 144 | +// output of the toString() method being called on |
| 145 | +// these objects |
| 146 | +console.log(civic.toString()); |
| 147 | +console.log(mondeo.toString()); |
| 148 | + |
| 149 | +//*******************************************************// |
| 150 | +// Constructors With Prototypes |
| 151 | +//*******************************************************// |
| 152 | + |
| 153 | +//********************** Snippet 1 **********************// |
| 154 | +// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance |
| 155 | +// [ES2015+] We used new constructor method and method declaration |
| 156 | +// [ES2015+] All of it new syntax sugar above old function structures |
| 157 | +class Car { |
| 158 | + constructor(model, year, miles) { |
| 159 | + this.model = model; |
| 160 | + this.year = year; |
| 161 | + this.miles = miles; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// Note here that we are using Object.prototype.newMethod rather than |
| 166 | +// Object.prototype so as to avoid redefining the prototype object |
| 167 | +// [ES2015+] We still could use Object.prototype for adding new methods, because internally we use the same structure |
| 168 | +// [ES2015+] We used new template literals for string interpolation |
| 169 | +Car.prototype.toString = function() { |
| 170 | + return `${this.model} has done ${this.miles} miles`; |
| 171 | +}; |
| 172 | + |
| 173 | +// Usage: |
| 174 | +// [ES2015+] We used new keyword const for immutable constant declaration |
| 175 | +const civic = new Car('Honda Civic', 2009, 20000); |
| 176 | +const mondeo = new Car('Ford Mondeo', 2010, 5000); |
| 177 | + |
| 178 | +console.log(civic.toString()); |
| 179 | +console.log(mondeo.toString()); |
0 commit comments