Finished Chapter 5.4 Object References
parent
57ade8fe71
commit
b10bb9aba7
|
@ -20,3 +20,10 @@ Parse objects into function just like normal number, it allow us to parse multip
|
||||||
|
|
||||||
Exmple check [object-function.js](../src/objects/objects-functions.js)
|
Exmple check [object-function.js](../src/objects/objects-functions.js)
|
||||||
|
|
||||||
|
## Object References
|
||||||
|
|
||||||
|
When a object is parsed as argument into function, it's parsed as reference (not as a copy). Changes done by function on object affect original object
|
||||||
|
|
||||||
|
But when a new value to the argument (in function), it break the binding (pass as reference) within function. But does not affect the original object
|
||||||
|
|
||||||
|
Example check [object-references.js](../src/objects/objects-references.js)
|
|
@ -0,0 +1,20 @@
|
||||||
|
let myAccount = {
|
||||||
|
name: "Andrew Mead",
|
||||||
|
expenses: 0,
|
||||||
|
income: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
let addExpense = function (account, amount) {
|
||||||
|
account.expenses = account.expenses + amount
|
||||||
|
console.log(account)
|
||||||
|
}
|
||||||
|
|
||||||
|
addExpense(myAccount, 2.5) // => { name: 'Andrew Mead', expenses: 2.5, income: 0 }
|
||||||
|
addExpense(myAccount, 5.2) // => name: 'Andrew Mead', expenses: 7.7, income: 0 }
|
||||||
|
|
||||||
|
addExpense = function (account, amount) {
|
||||||
|
account = {}
|
||||||
|
console.log(account) // => {} as it break binding
|
||||||
|
}
|
||||||
|
addExpense(myAccount, 2.5)
|
||||||
|
console.log(myAccount) // => { name: 'Andrew Mead', expenses: 7.7, income: 0 }
|
Loading…
Reference in New Issue