0

I am trying to code an object with a pointer-like behaviour. It is not meant to be a real pointer as used in other languages, but I need something that refers to a piece of data, so that I can pass that thing around and be sure it is always refering to the very same piece of data.

Consider the following code:

var Pointer = function (data) {
    var instance = {};
    instance.deref = function () { return data; };
    return instance;
};

What I am doing now, is storing the data in the scope of the pointer instance.

My question is: Does storing the data in the scope of the pointer have any significant downsides in terms of speed when I have to move a bunch of pointers from one place to the other? Imagine a data structure where the data is represented by these pointers, all having a unique piece of data in their scope, and I have to manipulate the structure by moving pointers around.

1 Answer 1

3

Why all the trouble to wrap this in closures? Every object has pointer-like behavior. You won't get deep copies of objects unless you do the work to make one.

var x = {}, y;
x.data = "foo";
y = x;
y.data = "bar";  
console.log(x.data) // "bar" because y and x are references to the same object

Really, the only major behavior that differentiates JavaScript references from C-style pointers is the lack of pointer arithmetic or casting a pointer to/from an integer. You can definitely shuffle references to objects around in your data structure without creating copies of the objects being referenced.

In the code above, if we're being precise in our language, x and y are not objects, x and y are references (very similar to pointers) to objects. When we assign an empty object to x, x now refers to (points at) an empty object. We later set y to refer to (point at) the same object as x.

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

3 Comments

Additionally, OP might want to declare that object const which does exactly that - guarantee that 'it is always refering to the very same piece of data'.
I used a closure to make the data sort of like a private property of the pointer and have control over the way the data can be accessed. My example is simplified. I am aware of things like Object.defineProperty(), preventExtensions(), seal() and freeze(), and I have to look into this to see if there is a better way. I don't need to do pointer arithmetics or use the address, so I don't mind the lack of this.
I guess I could have simplified my example further and do what you did. Then I would probably have realized it is not the data itself that is in the scope of the pointer, but also a reference. Your example shows that clearly and it answered my question. However, I have a number of data structures I want to make and for my project it makes sense to give something like this a name, like pointer (or something else). This makes clear the only purpose and capability of the object is to refer to a piece of data somewhere else. Instead of being an object that does who knows what.

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.