Skip to content

Commit df0b973

Browse files
committed
object litterals lessons done
1 parent 34fe2dd commit df0b973

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

app/index.js

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1+
// function createBookShop(inventory) {
2+
// return {
3+
// inventory: inventory,
4+
// inventoryValue: function() {
5+
// return this.inventory.reduce((total, book)=> total + book.price, 0 );
6+
// },
7+
// priceForTitle: function(title) {
8+
// return this.inventory.find(book => book.title === title).price;
9+
// }
10+
// }
11+
// }
12+
//
13+
// const inventory = [
14+
// { title: 'Harry Potter', price: 10 },
15+
// { ttile: "Eloquent JS", price: 15 }
16+
// ];
17+
//
18+
// const bookShop = createBookShop(inventory);
19+
//
20+
// console.log(bookShop.inventoryValue());
21+
// console.log(bookShop.priceForTitle('Harry Potter'));
22+
23+
24+
//rewritten with Obj Literals
25+
//if key and value are exact same name, we can just say it one time. (ie inventory :inventory is just inventory
26+
//if you have a key value pair this is a function, you can omit the function keyword and the :
127
function createBookShop(inventory) {
228
return {
3-
inventory: inventory,
4-
inventoryValue: function() {
29+
inventory,
30+
inventoryValue() {
531
return this.inventory.reduce((total, book)=> total + book.price, 0 );
632
},
7-
priceForTitle: function(title) {
33+
priceForTitle(title) {
834
return this.inventory.find(book => book.title === title).price;
935
}
1036
}
@@ -15,4 +41,37 @@ const inventory = [
1541
{ ttile: "Eloquent JS", price: 15 }
1642
];
1743

18-
const bookShop = createBookShop(inventory);
44+
const bookShop = createBookShop(inventory);
45+
46+
console.log(bookShop.inventoryValue());
47+
console.log(bookShop.priceForTitle('Harry Potter'));
48+
49+
50+
//another set
51+
// function saveFile(url, data) {
52+
// $.ajax({
53+
// method: 'POST',
54+
// url: url,
55+
// data: data
56+
// });
57+
// }
58+
//
59+
// const url = "http://fileupload.com";
60+
// const data = { color: red };
61+
//
62+
// saveFile(url, data);
63+
64+
//again if key and value are same you can just call it one time
65+
//it is standard to move single key value pairs to the left/top of list
66+
function saveFile(url, data) {
67+
$.ajax({
68+
url,
69+
data,
70+
method: 'POST',
71+
});
72+
}
73+
74+
const url = "http://fileupload.com";
75+
const data = { color: red };
76+
77+
saveFile(url, data);

0 commit comments

Comments
 (0)