Finished Chapter 5.3 Using Objects with Functions

This commit is contained in:
Jason Zhu 2021-01-19 21:51:23 +11:00
parent 6c7d7a0ed2
commit 57ade8fe71
2 changed files with 52 additions and 1 deletions

View File

@ -13,3 +13,10 @@ let object_name = {
To access a single property, using `.` (dot) operator
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)

View File

@ -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)