Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript how to create reference

Could you propose any workarounds to implement a reference to variable 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

What about passing context as first argument and pass variable name (as string) and later somehow evaluate that reference in predefined context. Is this feasible?

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());
like image 329
Mike Avatar asked Apr 28 '11 19:04

Mike


People also ask

How do you use references in JavaScript?

The Bottom Line on JavaScript ReferencesOn variable assignment, the scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are assigned-by-value and compound values are assigned-by-reference. The references in JavaScript only point at contained values and NOT at other variables, or references.

How do you store references in JavaScript?

JavaScript always pass by value. And everything is an object; var stores the pointer, hence it's pass by pointer's value. If your name = "bar" is supposed to be inside a function, you'll need to pass in the whole array instead. The function will then need to change it using array["reference"] = "bar" .

What is a reference type in JavaScript?

Reference data types, unlike primitive data types, are dynamic in nature. That is, they do not have a fixed size. Most of them are considered as objects, and therefore have methods. Examples of such data types include arrays, functions, collections, and all other types of objects.

Is JavaScript copy by reference?

In JavaScript primitive types are copied and passed by value and objects are copied and passed by reference value.


2 Answers

You have to use a string of the variable name but I think this is as close as you'll ever get 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 scenario it would be better to use a closure directly, so that you don't have to use a string of the variable name:

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
like image 56
brianpeiris Avatar answered Sep 19 '22 09:09

brianpeiris


In JavaScript, you can't pass primitive values (numbers, strings, etc) by reference. However, every object you pass will always be by reference. (this includes arrays)

To use your example:

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

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

foo.x = 6;

// foo.x => 6
// refFoo.x => 6
like image 26
Dominic Barnes Avatar answered Sep 21 '22 09:09

Dominic Barnes