javascript how to create reference

后端 未结 5 1744
孤独总比滥情好
孤独总比滥情好 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: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
    

提交回复
热议问题