Skip to content

Commit bc7a8ac

Browse files
committed
default function assignments done
1 parent df0b973 commit bc7a8ac

File tree

1 file changed

+35
-64
lines changed

1 file changed

+35
-64
lines changed

app/index.js

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,48 @@
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'));
1+
//Default Funciton Paramaeters
222

3+
function makeAjaxRequest(url, method) {
234

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 :
27-
function createBookShop(inventory) {
28-
return {
29-
inventory,
30-
inventoryValue() {
31-
return this.inventory.reduce((total, book)=> total + book.price, 0 );
32-
},
33-
priceForTitle(title) {
34-
return this.inventory.find(book => book.title === title).price;
35-
}
5+
if (!method) {
6+
method = 'Get';
367
}
8+
9+
//logic for request
10+
3711
}
3812

39-
const inventory = [
40-
{ title: 'Harry Potter', price: 10 },
41-
{ ttile: "Eloquent JS", price: 15 }
42-
];
13+
makeAjaxRequest('google.com');
14+
makeAjaxRequest('goole.com', 'GET');
15+
16+
17+
//This says if user did not pass in a method argument, it is assgined 'GET' otherwise method is whatever is sent in.
18+
//if you want it to be undefined specifically you can pass in null instead.
19+
function makeAjaxRequest(url, method = 'GET') {
4320

44-
const bookShop = createBookShop(inventory);
21+
//logic for request
4522

46-
console.log(bookShop.inventoryValue());
47-
console.log(bookShop.priceForTitle('Harry Potter'));
23+
}
24+
25+
makeAjaxRequest('google.com');
26+
makeAjaxRequest('google.com', 'POST');
27+
makeAjaxRequest('google.com', null);
4828

4929

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);
30+
//__________________________
6331

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-
});
32+
function User(id) {
33+
this.id = id;
7234
}
7335

74-
const url = "http://fileupload.com";
75-
const data = { color: red };
36+
function generateID() {
37+
return Math.random() * 999999;
38+
}
39+
//this makes use of default arguements, if a user isn't passed in, it generates a user to make admin. So can
40+
//work either way as is called below;
41+
function createAdminUser(user = new User(generateID())) {
42+
user.admin = true;
43+
return user;
44+
}
7645

77-
saveFile(url, data);
46+
createAdminUser();
47+
const user = new User(generateID());
48+
createAdminUser(user);

0 commit comments

Comments
 (0)