0

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]
}
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
Timar Ivo Batis
  • 1,861
  • 17
  • 21
  • if `returnObject()` is a function then `returnObj() = { yellow : 'house'}` is invalid syntax. Assignments involve a variable (ie `var`, `let`, or `const`), primitives, data types, etc., – zer00ne Apr 02 '19 at 01:44
  • is the `data` object always in that given format? – wentjun Apr 02 '19 at 02:12
  • Also, what is your question? Are you trying to write a function to reassign an object to a new object via `returnObj()`? – wentjun Apr 02 '19 at 02:12
  • @wentjun i suppose so. there could be workarounds but nevertheless, would be interesting to see a way to do the following. – Timar Ivo Batis Apr 02 '19 at 09:24
  • I believe this is relevant: https://stackoverflow.com/questions/3709866/whats-a-valid-left-hand-side-expression-in-javascript-grammar It seems that this is less a question of whether the function returns a reference or a value, but rather what is valid as part of an assignment operator is javascript – OliverRadini Apr 02 '19 at 09:53

2 Answers2

0

You cannot assign a value to function in this way.You should pass parameters to function (returnObj) for example

var obj = {
color : 'green'
}

function returnObj(key , value){
  obj[key] = value;
  return obj;
}

obj = returnObj("size","big");
console.log(JSON.stringify(obj))
0

A function return always returns a value not a reference. Object entries and Array entries are references within that value.

var obj = { color : 'green' }

var reference_to_obj = obj
reference_to_obj.color = 'red'
console.log(obj.color)
>>>red

function return_obj(){return obj}
return_obj().color = "blue"
console.log(obj.color)
>>>blue

all three are referencing the same object-content

but while we can reassign the name "reference_to_obj" to another value

reference_to_obj = 'string'

the function return_obj() behaves like an 'Anonymous' reference to the object. Only aware of the content of the object/Array but oblivious to the variable name it was saved under.

return_obj() = {} //is similar to 
AnonymousReference_1231kf10h1kf1 = {} // pseudocode for illustration

Workarounds:

1.Store the dynamic reference in a variable. Unfortunately you have to remember to call the update function when the reference is supposed to change

var objectReference;
function updateObjectReference(){
objectReference = obj[setting]
}

2.Inner Nesting: one layer deeper

var obj = {content : {color: green}}
returnObj().content = {size: big}

3.Outer Nesting: Global Object Window.

window[returnObjectNameString()]={size: big}

4. Function returns object but is also able to replace it.

function returnObj(replacementObj){
if(typeof replacementObj !== 'undefined'){ obj = replacementObj }
return obj
}

5. Delete all values and assign new ones. Note: it will remain an object.

var props = Object.keys(returnObj());
for (var i = 0; i < props.length; i++) {
  delete returnObj()[props[i]];
}
Timar Ivo Batis
  • 1,861
  • 17
  • 21