Finished Chapter 5.3 Using Objects with Functions
This commit is contained in:
parent
6c7d7a0ed2
commit
57ade8fe71
@ -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)
|
||||
|
||||
|
44
src/objects/objects-functions.js
Normal file
44
src/objects/objects-functions.js
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user