|
| 1 | +slidenumbers: true |
| 2 | +autoscale: true |
| 3 | +build-lists: true |
| 4 | +theme: work, 7 |
| 5 | + |
| 6 | +[.hide-footer] |
| 7 | + |
| 8 | +# JavaScript Prototypal Inheritance — a Brief Intro |
| 9 | + |
| 10 | +Roy Vanegas |
| 11 | + |
| 12 | +#### GothamSass |
| 13 | +#### New York, NY |
| 14 | +#### 21 September 2017 |
| 15 | + |
| 16 | +^ |
| 17 | +* Logitech Spotlight |
| 18 | +* Projector resolution |
| 19 | +* 16:9 (78%, or 1.7 repeating) 1080p/i; 720p/i |
| 20 | +* 4:3 (33%, or 1.3 repeating) |
| 21 | +* Set aspect ratio under the Presentation menu. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Objectives |
| 26 | + |
| 27 | +We’ll learn |
| 28 | + |
| 29 | +* how to create objects that inherit from other objects; |
| 30 | +* about the base prototype and how other objects inherit from it; |
| 31 | +* and, how and when to use inheritance. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Agenda |
| 36 | + |
| 37 | +We’ll see |
| 38 | + |
| 39 | +* examples of the five `Object.prototype` methods that all objects inherit from, |
| 40 | +* an example of prototypal inheritance in the real world. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Definitions |
| 45 | + |
| 46 | +* **Object**: a composite of data types, including other objects. |
| 47 | +* **Method**: a function bound to an object. |
| 48 | +* **Prototype**: a template, or model, consisting of methods used by other objects. |
| 49 | +* **Inheritance**: properties inherited by a “child” instance of another object. |
| 50 | + |
| 51 | +^ |
| 52 | +Use markers |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Prototypal Inheritance |
| 57 | + |
| 58 | +* Also known as prototype chaining. |
| 59 | +* `Object.prototype` is the first in the prototype chain. |
| 60 | +* `Object.prototype` is the base prototype from which all objects inherit. |
| 61 | +* `Object.prototype` provides **five** methods to all descendants: |
| 62 | + |
| 63 | +^ |
| 64 | +Make an analogy with human lineage. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Prototypal Inheritance |
| 69 | +### `hasOwnProperty` |
| 70 | + |
| 71 | +`hasOwnProperty()` returns `true` on non-inherited properties it owns, or `false` for inherited properties. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Prototypal Inheritance |
| 76 | +### `hasOwnProperty` |
| 77 | + |
| 78 | +````javascript |
| 79 | +let dog = { |
| 80 | + cute: true |
| 81 | +}; |
| 82 | + |
| 83 | +dog.hasOwnProperty('ugly'); // false |
| 84 | +dog.hasOwnProperty('cute'); // true |
| 85 | +dog.hasOwnProperty('toString'); // false |
| 86 | +Object.prototype.hasOwnProperty('toString'); // true |
| 87 | +```` |
| 88 | + |
| 89 | +^ toString is another of the five base methods. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Prototypal Inheritance |
| 94 | +### `propertyIsEnumerable` |
| 95 | + |
| 96 | +`propertyIsEnumerable()` returns `true` on non-inherited properties defined as `enumerable` (the default), which means it will appear in a `for...in` loop. It returns `false` otherwise. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Prototypal Inheritance |
| 101 | +### `propertyIsEnumerable` |
| 102 | + |
| 103 | +````javascript |
| 104 | +let dog = { |
| 105 | + adoptable: true, |
| 106 | + vaccinated: true |
| 107 | +}; |
| 108 | + |
| 109 | +Object.defineProperty(dog, 'name', { |
| 110 | + value: 'Spot', |
| 111 | + enumerable: false |
| 112 | +}); |
| 113 | + |
| 114 | +dog.propertyIsEnumerable('adoptable'); // true |
| 115 | +dog.propertyIsEnumerable('vaccinated'); // true |
| 116 | +dog.propertyIsEnumerable('name'); // false |
| 117 | + |
| 118 | +console.log('Please adopt ' + dog.name + '. He’s…'); |
| 119 | + |
| 120 | +for (let property in dog) { |
| 121 | + console.log('— ' + property); |
| 122 | +} |
| 123 | +```` |
| 124 | + |
| 125 | +^ |
| 126 | +* enumerable is true by default, thus the need to use defineProperty |
| 127 | +* Questions? |
| 128 | +* Why do you think enumerable is true by default? |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Prototypal Inheritance |
| 133 | +### `valueOf` |
| 134 | + |
| 135 | +`valueOf()` returns the primitive value associated with an object, or the object itself. Only in the most unique and isolated situations would you modify this method. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Prototypal Inheritance |
| 140 | +### `valueOf` |
| 141 | + |
| 142 | +````javascript |
| 143 | +// Construct the Dog object |
| 144 | +function Dog(name, breed) { |
| 145 | + this.name = name; |
| 146 | + this.breed = breed; |
| 147 | +} |
| 148 | + |
| 149 | +// Create a new Dog instance named dog |
| 150 | +let dog = new Dog('Spot', 'mutt'); |
| 151 | + |
| 152 | +// Retrieve the object itself |
| 153 | +dog.valueOf(); // Dog {name: "Spot", breed: "mutt"} |
| 154 | + |
| 155 | +// Using prototypal inheritance, over-ride how valueOf behaves, returning the name of the dog |
| 156 | +Dog.prototype.valueOf = function () { |
| 157 | + return this.name; |
| 158 | +} |
| 159 | + |
| 160 | +dog.valueOf(); // Spot |
| 161 | +```` |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## Prototypal Inheritance |
| 166 | +### `toString` |
| 167 | + |
| 168 | +`toString()` converts an object to a string whenever the object is used in a string context. The result, however, many not be useful, so over-riding it using prototypal inheritance can be advantageous. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Prototypal Inheritance |
| 173 | +### `toString` |
| 174 | + |
| 175 | +````javascript |
| 176 | +let dog = { |
| 177 | + adoptable: true, |
| 178 | + vaccinated: false |
| 179 | +}; |
| 180 | + |
| 181 | +dog.toString(); // [object Object] |
| 182 | + |
| 183 | +// Over-ride the inherited toString method in the prototype |
| 184 | +dog.toString = function () { |
| 185 | + let output = 'This dog is '; |
| 186 | + |
| 187 | + output += ((this.adoptable) ? 'adoptable ' : 'not adoptable '); |
| 188 | + output += ((this.vaccinated)? 'and vaccinated.' : 'and not vaccinated.'); |
| 189 | + |
| 190 | + return output; |
| 191 | +} |
| 192 | + |
| 193 | +dog.toString(); // This dog is adoptable and not vaccinated. |
| 194 | +dog.hasOwnProperty('toString'); // true, because toString shadows the inherited prototype method for toString |
| 195 | +```` |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +## Prototypal Inheritance |
| 200 | +### `isPrototypeOf` |
| 201 | +`isPrototypeOf()` returns `true` if one object is the prototype of another, or `false` if otherwise. (`isPrototypeFor` is a more appropriate name. You’ll see why in a moment.) |
| 202 | + |
| 203 | +--- |
| 204 | + |
| 205 | +## Prototypal Inheritance |
| 206 | +### `isPrototypeOf` |
| 207 | + |
| 208 | +````javascript |
| 209 | +let dog = { |
| 210 | + adoptable: true, |
| 211 | + vaccinated: false |
| 212 | +}; |
| 213 | + |
| 214 | +let puppy = Object.create(dog); |
| 215 | + |
| 216 | +// Is Object.prototype a prototype of dog? |
| 217 | +Object.prototype.isPrototypeOf(dog); // true |
| 218 | + |
| 219 | +// Is dog a prototype of puppy? |
| 220 | +dog.isPrototypeOf(puppy); // true |
| 221 | + |
| 222 | +// The prototype chain, or prototypal inheritance, looks like… |
| 223 | +'Object.prototype → dog → puppy' |
| 224 | +```` |
| 225 | + |
| 226 | +--- |
| 227 | + |
| 228 | +## Inheritance in the Real World |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +--- |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | +## [fit] Demo Time |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | +--- |
| 253 | + |
| 254 | +## [fit] Thanks |
0 commit comments