2

Is it possible to keep an object reference without using an holder object in javascript? Currently when an object gets overridden I sometimes lose the reference to the "current" object state illustrated in the snippet below;

Is there a way to put a "pointer" in an array or not?

EDIT To the questions asked:

What I have in the objects I have are references to form fields. Some of these are text fields, some of them are textareas, some of them checkboxes.

I wish to keep a map next to the direct referene of what type they are.

basicaly it would be

obj {
   this.text1 = createTextField();
   this.text1.datepicker();
   this.text2 = createTextField();
   this.area1 = createArea();
   this.check = createCheck();
   this.datefields = [this.text1];
   this.checkboxes = [this.check];
}

So I can use the datefields/checkboxes array as a checkpoint to validate against which type a field is/should behave.

Currently I use

function datefields() { return [this.text1]; };

But I'd like to know if there's a better way to do this than to intantiate a new array when I need to check it.

I know there is a way with observers to mimic pointer behaviour, and i've fiddled with those and have some good results with that, i'm just curious if there are other ways i'm not aware of.

function myObject() {
    this.myvalue = null;
    this.arr = [this.myvalue];
}

myObject.prototype.alter = function() {
    this.myvalue = "hello";
}

var x = new myObject();
var elem = document.getElementById('results');

function log(message) {
    elem.appendChild(document.createTextNode(message));
    elem.appendChild(document.createElement('br'));
}

log("x.myvalue = "+x.myvalue);
log("x.arr[0] = "+x.arr[0]);
log("calling alter");
x.alter();
log("x.myvalue = "+x.myvalue);
log("x.arr[0] = "+x.arr[0]);   
<div id="results"></div>
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 2
    whenever the root obj is changed the, the reference link between the root and the reference will be void – Strikers Jun 08 '15 at 14:39
  • Please use `console.log()` for simplicity – Nayuki Jun 08 '15 at 14:42
  • @NayukiMinase `console.log()` for code snippets, really? –  Jun 08 '15 at 14:44
  • it's simply not possible, read this http://stackoverflow.com/questions/10231868/pointers-in-javascript (don't read only the accepted answer) –  Jun 08 '15 at 14:45
  • are you trying to keep a history of myvalues in the array? –  Jun 08 '15 at 14:46
  • @OP: Does it help if you add to the code `this.arr.push(this.myvalue)` into function `alter`? – Nayuki Jun 08 '15 at 14:47
  • 1
    So you want to point `myObject.arr[0]` to `myObject.myvalue` and update the `.arr` whenever `.myvalue` changes? You could implement it using getters and setters, for example: http://jsfiddle.net/xcu7ku2m/ - not posting this as an answer since I'm not clear if it's what you're looking for. – pawel Jun 08 '15 at 14:51
  • I have modified my question to give some more insight to the purpose of my question. – Tschallacka Jun 08 '15 at 15:01
  • @MichaelDibbets I think you get things mixed up. The values stored in `this.datefields/checkboxes` and `this.arr` are not of the same kind. Indeed, the first one contains objects (form fields) with attributes and methods, while the second one contains primitve values. Thus I think you should consider them separately. However, I'd like to see a real code sample where your problem occurs. –  Jun 08 '15 at 15:30

2 Answers2

1

Simple answer: Only objects (including all subtypes) are passed by reference in JS. All other simple values are copied.

For a bit more detail I would recommend reading You Don't Know JS: Types & Grammer but specifically the section Value vs Reference in Chapter 2:

In JavaScript, there are no pointers, and references work a bit differently. You cannot have a reference from one JS variable to another variable. That's just not possible.

Quoting further on:

Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.

Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.

There are plenty of examples included to show these points. I would highly recommend reading through to get a better understanding of how values/references work in JS.

Community
  • 1
  • 1
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • I know exactly how the referencing works and how to use it. I'm just curious if theres mechanics with observers or something else to mimic pointer behaviour – Tschallacka Jun 08 '15 at 17:52
0

There is no pointers in Javascript, though you could cheat a little using a wrapper object. Here is a minimal implementation of such an object:

var Wrapper = function (value) {
    this.value = value;
};

Wrapper.prototype.valueOf = function () {
    return this.value;
};

Then you may use it in place of the original value:

function myObject() {
    this.myvalue = new Wrapper(null); // wrapper
    this.arr = [this.myvalue];
}

myObject.prototype.alter = function() {
    this.myvalue.value = "hello"; // notice the ".value"
}

The rest of your code needs no tweaks.

  • Yea, but this does exactly what I don't want to do. See the first line in my question ;-) Thanks for the effort though. – Tschallacka Jun 08 '15 at 15:02
  • @MichaelDibbets Indeed! Sorry then :-D Anyway, you can't do this using Javascript :-/ –  Jun 08 '15 at 15:09