83 lines
1.4 KiB
JavaScript
83 lines
1.4 KiB
JavaScript
var attrs, coffee, coffees, to_print;
|
|
|
|
coffee = {
|
|
name: 'French',
|
|
strength: 1
|
|
};
|
|
|
|
console.log(coffee);
|
|
|
|
// Spaced-out writing
|
|
coffee = {
|
|
name: 'French',
|
|
strength: 1
|
|
};
|
|
|
|
console.log(coffee);
|
|
|
|
coffee = {
|
|
name: 'French',
|
|
strength: 1,
|
|
brew: function() {
|
|
return console.log(`Brewing ${this.name}`);
|
|
},
|
|
pour: function(amount = 1) {
|
|
if (amount === 1) {
|
|
return "Poured a single cup";
|
|
} else {
|
|
return `Poured ${amount} cups`;
|
|
}
|
|
}
|
|
};
|
|
|
|
coffee.brew();
|
|
|
|
console.log(coffee.pour());
|
|
|
|
console.log(coffee.pour(2));
|
|
|
|
coffees = {
|
|
french: {
|
|
strength: 1,
|
|
in_stock: 20
|
|
},
|
|
italian: {
|
|
strength: 2,
|
|
in_stock: 12
|
|
},
|
|
decaf: {
|
|
strength: 0,
|
|
in_stock: 0
|
|
}
|
|
};
|
|
|
|
console.log(coffees);
|
|
|
|
for (coffee in coffees) {
|
|
attrs = coffees[coffee];
|
|
// For key, value of object. Notice how the "for" applies to everything to its
|
|
// left, including the console.log.
|
|
console.log(`${coffee} has ${attrs.in_stock}`);
|
|
}
|
|
|
|
// It compiles exactly like this:
|
|
for (coffee in coffees) {
|
|
attrs = coffees[coffee];
|
|
console.log(`${coffee} has ${attrs.in_stock}`);
|
|
}
|
|
|
|
to_print = (function() {
|
|
var results;
|
|
results = [];
|
|
for (coffee in coffees) {
|
|
attrs = coffees[coffee];
|
|
if (attrs.in_stock > 0) {
|
|
results.push(`${coffee} has ${attrs.in_stock}`);
|
|
}
|
|
}
|
|
return results;
|
|
})();
|
|
|
|
console.log(to_print.join(", "));
|
|
|
|
//# sourceMappingURL=4-4-objects.js.map
|