creating an object and returning it from a function works.
var obj = {
color : 'green'
}
function returnObj(){
return obj
}
console.log(JSON.stringify(obj))
>>>{color : 'green'}
adding a new key value pair in this manner works. entries of returned values like objects or arrays are references.
returnObj().size = "big"
console.log(JSON.stringify(obj))
>>>{color : 'green', size : 'big'}
reassigning it a new object doesn't work though.
returnObj() = { yellow : 'house'}
>>> ReferenceError: Invalid left-hand side in assignment
What i would like to do is to force the function to return an l-value instead of an r-value. The following doesn't work either.
returnObj().this = { yellow : 'house'}
console.log(JSON.stringify(obj))
>>>{"color":"green","size":"big","this":{"yellow":"house"}}
The reason for doing this is that, depending on user settings there are different objects to be referenced.
var data = {
house: {color: 'green'}
car : {speed: "fast"},
}
var setting = 'house'
function returnDataObj(){
return obj[setting]
}