Created
July 24, 2017 16:56
-
-
Save danman01/659fe05a524dd5a8b12885134686cb06 to your computer and use it in GitHub Desktop.
changing props of reference type within function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> const menu = { | |
... 'burger': 5.99, | |
... 'salad': 4.99, | |
... 'drink': 1.00 | |
... } | |
undefined | |
> menu | |
{ burger: 5.99, salad: 4.99, drink: 1 } | |
> const addProperty = function addProperty(obj, prop, val) { | |
... obj[prop] = val | |
... console.log(`added ${prop} with val ${val} to the object`) | |
... } | |
undefined | |
> addProperty | |
[Function: addProperty] | |
> menu | |
{ burger: 5.99, salad: 4.99, drink: 1 } | |
> addProperty(menu, 'dessert of the day - cookie', '3.00') | |
added dessert of the day - cookie with val 3.00 to the object | |
undefined | |
> menu | |
{ burger: 5.99, | |
salad: 4.99, | |
drink: 1, | |
'dessert of the day - cookie': '3.00' } | |
> addProperty(menu, entree, 15) | |
ReferenceError: entree is not defined | |
at repl:1:19 | |
at realRunInThisContextScript (vm.js:22:35) | |
at sigintHandlersWrap (vm.js:98:12) | |
at ContextifyScript.Script.runInThisContext (vm.js:24:12) | |
at REPLServer.defaultEval (repl.js:346:29) | |
at bound (domain.js:280:14) | |
at REPLServer.runBound [as eval] (domain.js:293:12) | |
at REPLServer.onLine (repl.js:545:10) | |
at emitOne (events.js:101:20) | |
at REPLServer.emit (events.js:188:7) | |
> addProperty(menu, 'entree', 15) | |
added entree with val 15 to the object | |
undefined | |
> menu | |
{ burger: 5.99, | |
salad: 4.99, | |
drink: 1, | |
'dessert of the day - cookie': '3.00', | |
entree: 15 } | |
> addProperty(menu, 'entree number 2', 25) | |
added entree number 2 with val 25 to the object | |
undefined | |
> menu | |
{ burger: 5.99, | |
salad: 4.99, | |
drink: 1, | |
'dessert of the day - cookie': '3.00', | |
entree: 15, | |
'entree number 2': 25 } | |
> menu.'dessert of the day' = 5 | |
menu.'dessert of the day' = 5 | |
^^^^^^^^^^^^^^^^^^^^ | |
SyntaxError: Unexpected string | |
> menu['dessert of the day'] = 5 | |
5 | |
> menu | |
{ burger: 5.99, | |
salad: 4.99, | |
drink: 1, | |
'dessert of the day - cookie': '3.00', | |
entree: 15, | |
'entree number 2': 25, | |
'dessert of the day': 5 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment