Javascript how to create a link

Could you suggest any workarounds for implementing a variable reference using closures or any other tricks?

createReference = function() {
    // TODO: how to implement?
};

var x = 5;
var refX = createReference(x); // could be any parameters needed to implement the logic
x = 6;
alert(refX()); // should alert 6

As for passing the context as the first argument and the variable name pass (as a string), and then somehow evaluate this link in a predefined context. Is it possible?

Here's a more complete scenario:

createReference = function(context, prop) {
    return function() {
        return context[prop];
    };
};

Provider = function() {
};
Provider.prototype.x = 5;
Provider.prototype.getXRef = function() {
    return createReference(this, 'x');
};
Provider.prototype.incrementX = function() {
    this.x = this.x + 1;
};

var provider = new Provider();
var refX = provider.getXRef();
provider.incrementX();
alert(refX());
+5
source share
5 answers

You should use a variable name string, but I think it will be as close as you ever have in JavaScript:

var createReference = function (context, prop) {
  return function () { return context[prop]; };
};

var x = 5;
var refX = createReference(this, 'x');
x = 6;

alert(refX()); // alerts 6

Edit:

In your updated script, it would be better to use closure directly, so you don't need to use a variable name string:

var createReference = function (context, func) {
    return function () { return func.call(context); }
};

Provider = function() {
};
Provider.prototype.x = 5;
Provider.prototype.getXRef = function() {

    return createReference(this, function () { return this.x; });

    // OR if you happen to be running in a 
    // JavaScript 1.8 environment like Firefox 3+,
    // you can use "expression closures" for more
    // concise code:

    // return createReference(this, function () this.x);
};
Provider.prototype.incrementX = function() {
    this.x = this.x + 1;
};

var provider = new Provider();
var refX = provider.getXRef();
provider.incrementX();
alert(refX()); // alerts 6
+5

JavaScript (, ..) . , . ( )

:

var foo = { x: 5 };
var refFoo = foo;

// foo.x => 5
// refFoo.x => 5

foo.x = 6;

// foo.x => 6
// refFoo.x => 6
+2

You can’t just promote xas a link.

0
source

Only non-scalar types can be passed as a link and will always be passed as a link:

var reference = {};
my_function(reference);
console.log(reference); // will show property - a property value

function my_function(my_reference) {
    my_reference.property = "a property value";
}

var not_a_reference = [];
my_function(not_a_reference);
console.log(not_a_reference); // will NOT show 'a value'

function my_function() {
    my_reference.push("a value");
}

Closer to your example:

function show(value) {
    alert(value.data);
}

var value = { 'data': 5 };
show(value); // alerts 5
value.data = 6;
show(value); // alerts 6
0
source

Since objects will always be a static link, you can do this:

var o = {};
o.x = 5;
var oRef = o;
alert(oRef.x); // -> 5
o.x = 6;
alert(oRef.x); // -> 6
0
source

All Articles