0

In many other programming languages, one can get the value of a variable either as a copy or by reference.

Example (PHP):

$value = 'a value';
$copy = $value;
$ref = &$value;

$value = 'new value';
echo $copy; // 'a value'
echo $ref; // 'new value'

Is there a similar feature in JavaScript? Can I reference i.e. an object's property and even if I replace the property with a new string/object, the referring variable will point to the new data?

Here is an example code of what I am trying to achieve:

var obj = { property: 'a value' }
var another = obj.property // save this by reference to this variable
obj.property = 'new value'
console.log(another) // expected: 'new value'

So far I have tried using getters/setters, but this does not help since when I assign the property to a new variable, the getter is called and returns the actual value. I am quite sure this would be easy to implement using Node.js EventEmitter or by using function calls instead of properties, but I am really looking for some kind of language feature that I might have missed or for some obvious, easy way of doing these things.

Thanks!

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73

1 Answers1

0

In Javascript, objects are passed by refernce but primitives (strings, numbers and booleans) by value:

var obj = { property: 'a value' };
var another = obj.property; // value
obj.property = 'new value';
console.log(another); // gives a value

But

var obj = { property: 'a value' };
var another = obj; // reference
obj.property = 'new value';
console.log(another.property); // gives new value
Jakub Kotrs
  • 5,823
  • 1
  • 14
  • 30
  • Yes, that I know - I am looking for a way to get a reference to the variable holding the primitive. So that when I assign a completely new value to the variable, I still do not lose that reference. – Robert Rossmann Aug 27 '14 at 18:29
  • Well, you can use an object instead of a primitive, no? – Jakub Kotrs Aug 27 '14 at 18:30
  • @RobertRossmann JavaScript doesn't have pointers -- references to variables. [`get`/`set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty), as you mentioned in the question, is the closest equivalent currently supported by the language. – Jonathan Lonowski Aug 27 '14 at 18:31
  • I can.:) These are all options which I have considered. I am still looking to avoid that. For my current need, the object would have only a single property, which seems a bit of overkill. And it would introduce an unnecessary and, maybe, confusing nesting for the user, i.e. instead of `MyApp.configValue = 'new val'` -> `MyApp.config.configValue = 'new val`. To be honest, I will probably have to resort to one of the already known ways to handle this, but I wanted to ensure I have discovered all possible solutions. – Robert Rossmann Aug 27 '14 at 18:33
  • @JonathanLonowski thanks, **pointer** is the word I could not remember! That was exactly what I was looking for. Now when I search, I clearly see this is a duplicate question... Thanks! – Robert Rossmann Aug 27 '14 at 18:40