javascript how to create reference

后端 未结 5 1663
孤独总比滥情好
孤独总比滥情好 2021-01-05 20:45

Could you propose any workarounds to implement a reference to variable using closures or any other tricks?

createReference = function() {
    // TODO: how to         


        
相关标签:
5条回答
  • 2021-01-05 21:26

    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
    
    0 讨论(0)
  • 2021-01-05 21:30

    You can't just promote x to be a reference.

    0 讨论(0)
  • 2021-01-05 21:34

    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
    
    0 讨论(0)
  • 2021-01-05 21:41

    Only non-scalar types can be passed as reference, and will always be passed as reference:

    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 讨论(0)
  • 2021-01-05 21:42

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

    var o = {};
    o.x = 5;
    var oRef = o;
    alert(oRef.x); // -> 5
    o.x = 6;
    alert(oRef.x); // -> 6
    
    0 讨论(0)
提交回复
热议问题