0

say I've got a variable which points to that memory, can I actually change the memory to completely new memory so that every other variable pointing to that memory now points to new memory?

var foo = function(obj){
    // I want to set obj to new memory
    obj = { bar: 'foo' }
}
var boo = function(obj){
    // can change properties to new memory
    obj.too = { hoo: 'doo' }
}

var zoo = { too: { woo: 'loo' } }

// no change of memory
console.log(zoo)
foo(zoo)
console.log(zoo)

// change of memory
console.log(zoo)
boo(zoo)
console.log(zoo)
lopu
  • 175
  • 1
  • 18
  • Search for "Call By Object Sharing" (this is how JavaScript works): tldr; assigning to a *parameter* has no effect on the caller, while *mutating an object* mutates that objet .. everywhere. – user2864740 Jan 02 '18 at 04:14
  • tldr; if the *same object* is passed, mutations in *one function* (that is, *reassignment of properties [or any reassignment of a property that is reachable from said object]*) will be visible everywhere. There is no "copy" of objects created when calling a function. – user2864740 Jan 02 '18 at 04:21
  • @user2864740 I know that which is why you can reassign properties of variables which affects the variable globally, but you can't change the actual variable memory globally. I mean you could loop over and delete all properties, and then loop over all the properties of your new object and `Object.assign` them but that only works with objects, you can't turn an object into an array, or an object into a number, or really anything else – lopu Jan 02 '18 at 04:25
  • That's correct, see ["Call By Object Sharing"](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) :-) "In particular it is not call by value because mutations of arguments performed by the called routine will be visible to the caller. And *[call by sharing] is not call by reference because **access is not given to the variables of the caller**, but merely to certain objects*" – user2864740 Jan 02 '18 at 04:27
  • yes but this question is about completely overwriting the data that is being pointed to so that all pointers now use the new data – lopu Jan 02 '18 at 04:28
  • Not possible (although you *may* be able to achieve a close-enough intent with a *closure* or modifying properties of the Global [aka `window`] object). See the link above. – user2864740 Jan 02 '18 at 04:29

4 Answers4

1

The short answer is "yes". When you have an object, any/all references to that instance will access the current state of that instance. But, understand that you can have many instances of the same object and what you do to one instance may not affect the others.

Here's a more simplified example:

var zoo = { 
  location: "San Diego"  
};

function getReference(){
  // All invocations of this function will get a reference to same instance
  return zoo; 
}

var o1 = getReference();  // reference to zoo instance is returned
var o2 = getReference();  // 2nd reference to zoo instance is returned

console.log(o1.location, o2.location); // San Diego, San Diego
o2.location = "NYC";
console.log(o1.location, o2.location); // NYC, NYC

// But, a reference to a different instance won't affect the others
var o3 = Object.create(zoo);
o3.location = "Washington DC";
console.log(o1.location, o2.location, o3.location); NYC, NYC, Washington DC
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • ECMAScript does not use the term "reference" in this fashion - talking about "references" in JS is often baggage from other languages and the semantics can be clearly defined without such. – user2864740 Jan 02 '18 at 04:16
  • but this isn't being able to say `o2 = { other: 'data' }` and then `console.log(o1) //-> { other: 'data' }` ? – lopu Jan 02 '18 at 04:16
  • @lopu *variables* are *always* eagerly evaluated in an expression, including as function arguments (and thus it is *never possible* to "pass a reference" in JavaScript); this answers argues that variables can evaluate to *objects which can be mutated*. This does not change the first part of the previous statement. (See the "Reference Specification Type" for rvalues which *doesn't* have an effect on the answer, but is a little divergent read.) – user2864740 Jan 02 '18 at 04:17
  • @user2864740 so it isn't possible to overwrite the memory that a variable is pointing to with only access to the variable keyword itself? – lopu Jan 02 '18 at 04:22
  • @lopu There is no "memory" in the "C stack" sense in JavaScript. The language operates at a different level of definition and implementation. So no, it's *not possible* to "overwrite memory" excepting as it's possible to re-assign *properties* (and properties are not variables!) of objects. There are *closures* in JavaScript (that is, a variable that has been "closed over" can be re-assigned within it's defined scope), but that's a different issue and has *no effect* on function arguments/parameters. – user2864740 Jan 02 '18 at 04:23
  • @user2864740 interesting, it's weird that there isn't a way to completely clear some memory, ie a group of properties, including prototype ones, and then reassigning new properties, even for a `Number` or `String`, because all these variables consist of is properties, the prototypes and the value getters and setters, so theoretically you have access to everything that is defining that variable globally as memory – lopu Jan 02 '18 at 04:27
  • @lopu The `global` object, aka `window`, which is the object on which "global variables are properties of", can be *modified* globally. This still is *not* the same as "modifying the memory" or "passing by reference" though. – user2864740 Jan 02 '18 at 04:31
  • yes but if the variable is pointing to a deep child of window, say `window.something.somethingelse` then changing it through `window` isn't possible because you only have the variable keyword pointing to the data and nothing else – lopu Jan 02 '18 at 04:42
  • @lopu If you wish to modify an object, *that* object must be supplied/known. It could be a simple "wrapper object" that is mutated. Regardless, this is how JavaScript works. (Again, maybe a *closure* would solve a an actual task/problem at hand..?) – user2864740 Jan 02 '18 at 04:54
1

Due to how javascript passes arguments to functions (see this answer), the answer is no. To accomplish what you want though, I would reassign the variable to the function's return value, i.e. zoo = foo(zoo); (assuming you change foo to return something).

csp713
  • 412
  • 3
  • 6
1

You can create a system that does it for you.

memorySlot1 = "one"
memorySlot2 = "two"

AssignSlot = function (memorySlot){
    return(
        {
            Slot : memorySlot,
            Content : (
                function(){
                    return (
                        window[
                            memorySlot
                        ]
                    )
                }
            )
        }
    )
}

ChangeSlotContent = function (obj,newContent){
    window[obj.Slot] =  newContent
}

console.log("______________")
var1 = AssignSlot("memorySlot1")
var2 = AssignSlot("memorySlot2")
var3 = AssignSlot("memorySlot2")
console.log(var1.Content() +" / "+ var2.Content() +" / "+ var3.Content()) // one / two / two

console.log("______________")
ChangeSlotContent(var2,"twotwo")
console.log(var1.Content() +" / "+ var2.Content() +" / "+ var3.Content()) // one / twotwo / twotwo

console.log("______________")
var1 = AssignSlot("memorySlot2")
console.log(var1.Content() +" / "+ var2.Content() +" / "+ var3.Content()) // twotwo / twotwo / twotwo
aaron
  • 39,695
  • 6
  • 46
  • 102
  • Same comments as elsewhere: this still relies on *an object being modified/mutated*. (And as such the term "memory" is dubious - perhaps: 'NamedPropertyBinder' would be a more fitting name?) – user2864740 Jan 02 '18 at 18:51
0

Memory wise there really is no guarantee (its an implementation detail). Just because you observe a new value from a reference doesn't mean the memory was overwritten (it might just as well be using new memory space and free old space). Assignment to variables is done by value (you set a new value to the reference). This means that you can't have a variable pointing out to another variable. So changing any variable will never result in a change to any other variable.

It's however possible to have any number of variables to hold a reference to a unique object with unique key-value pairs. In that way variables have access to the same object and changes to any of its properties can be observed in any variable that holds that object reference since each property is unique to each object.

MinusFour
  • 13,913
  • 3
  • 30
  • 39