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>

9
  • 2
    whenever the root obj is changed the, the reference link between the root and the reference will be void Commented Jun 8, 2015 at 14:39
  • Please use console.log() for simplicity Commented Jun 8, 2015 at 14:42
  • @NayukiMinase console.log() for code snippets, really? Commented Jun 8, 2015 at 14:44
  • it's simply not possible, read this stackoverflow.com/questions/10231868/pointers-in-javascript (don't read only the accepted answer) Commented Jun 8, 2015 at 14:45
  • 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: jsfiddle.net/xcu7ku2m - not posting this as an answer since I'm not clear if it's what you're looking for. Commented Jun 8, 2015 at 14:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.