1 2 3 4 5 6 7 8 9 10 11 12 |
let hash = { key1: "value 1", key2: 'value 2', key3: 'value 3' } console.log(hash['key1']); for (key in hash) { console.log('key:', key); console.log('value:', hash[key]); } |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
function Animal (name, energy) { // Animal property this.name = name this.energy = energy } Animal.prototype.eat = function (amount) { console.log(`${this.name} is eating.`) this.energy += amount } Animal.prototype.sleep = function (length) { console.log(`${this.name} is sleeping.`) this.energy += length } Animal.prototype.play = function (length) { console.log(`${this.name} is playing.`) this.energy -= length } function Dog (name, energy, breed) { // Dog property Animal.call(this, name, energy) this.breed = breed // property } Dog.prototype.eat = function (amount) { console.log(`${this.name} is eating.`) this.energy += amount } Dog.prototype.sleep = function (length) { console.log(`${this.name} is sleeping.`) this.energy += length } Dog.prototype.play = function (length) { console.log(`${this.name} is playing.`) this.energy -= length } Dog.prototype.bark = function () { console.log('Woof-Woof!') this.energy -= .1 } const charlie = new Dog('Charlie', 10, 'Goldendoodle'); console.log(charlie) // will display all properties // also will display prototype function properties // of parent and itself for(key in charlie) { console.log('key', key); } // The problem with a for...in loop is that it iterates through properties in the Prototype chain. // When you loop through an object with the for...in loop, // you need to check if the property belongs to the object. // You can do this with hasOwnProperty. for (var property in charlie) { if (charlie.hasOwnProperty(property)) { console.log(`charlie owns property ${property}`); } else { console.log(`${property} is part of charlie chain`); } } // output: // charlie owns property name // charlie owns property energy // charlie owns property breed // eat is part of charlie chain // sleep is part of charlie chain // play is part of charlie chain // bark is part of charlie chain const keys = Object.keys(charlie); console.log(keys); // ["name", "energy", "breed"] const values = Object.values(charlie); console.log(values); // ["Charlie", 10, "Goldendoodle"] const entries = Object.entries(charlie) console.log(entries); // output: // 0: (2) ["name", "Charlie"] // 1: (2) ["energy", 10] // 2: (2) ["breed", "Goldendoodle"] // The better way to loop through objects is first convert it into an array with one of these three methods. // Object.keys // Object.values // Object.entries // Then, you loop through the results like a normal array. // Note: Object.keys() does not return symbols // Object.getOwnPropertySymbols returns array of all symbols attached to your 'this' context |