diff --git a/notes/chapter5_javascript-objects.md b/notes/chapter5_javascript-objects.md index f2011f3..2e2e6cc 100644 --- a/notes/chapter5_javascript-objects.md +++ b/notes/chapter5_javascript-objects.md @@ -12,4 +12,11 @@ let object_name = { To access a single property, using `.` (dot) operator -Check code in [object-101.js](../src/objects/objects-101.js) \ No newline at end of file +Check code in [object-101.js](../src/objects/objects-101.js) + +## Using Objects with Functions + +Parse objects into function just like normal number, it allow us to parse multiple values into function as a single value. Or get multiple values as single value from a function + +Exmple check [object-function.js](../src/objects/objects-functions.js) + diff --git a/src/objects/objects-functions.js b/src/objects/objects-functions.js new file mode 100644 index 0000000..e43ccb5 --- /dev/null +++ b/src/objects/objects-functions.js @@ -0,0 +1,44 @@ +// Parse object into function +let myBook = { + title: '1984', + author: 'George Orwell', + pageCount: 326 +} + +let otherBook = { + title: 'A Peoples History', + author: 'Howard Zinn', + pageCount: 723 +} + +let getSummary = function (book) { + console.log(`${book.title} by ${book.author}`); +} + +getSummary(myBook) +getSummary(otherBook) + +// Getting object out of function +getSummary = function (book) { + return { + summary: `${book.title} by ${book.author}`, + pageCountSummary: `${book.title} is ${book.pageCount} pages long` + } +} + +console.log(getSummary(myBook)) +console.log(getSummary(otherBook)) + + +// Challenge area +// Create function - take fahrenheit in - return ojbect with all three +let converFahrenheit = function (fahrenheit) { + return { + fahrenheit: fahrenheit, + celsius: (fahrenheit - 32) * (5/9), + kelvin: (fahrenheit + 459.67) * (5/9) + } +} + +let temps = converFahrenheit(20); +console.log(temps) \ No newline at end of file