0

I'd like to declare some variables, an then let let a function build the actual objects and assign their reference to these previously declared variables.

The way I do it now works, but it gets tedious with many variables.

function bar(obj) {
    var newObject = {'an':'object'};
    obj['element'] = newObject;
}

function foo() {
    var ref={};
    bar(ref);
    ref=ref.element; // Tedious line
}

For those wondering why I would like to do such a thing, I built a simple function to generate the DOM from JSON. It goes something like that, with #bind used to do what bar does in the previous snippet :

var legend = {};
$('#element').append(['form',['fieldset',
    ['legend',{'#bind':legend},['text':'Legend title']]
    ]].getDomForThis());
legend=legend.element;

Now I can append some new elements after the legend with something like

for(fieldName in fields) {
  legend.insertAfter(['div',[['input',['name':fieldName]],...]].getDomForThis());
}

I'd like to avoid having to write the tedious line. It's like createElement and appendChild, one function call should be enough !

UPDATE : After some thought, I believe there is no way to do that with Javascript. I was hoping for some hidden Javascript magic somewhere, but I have reason to think it will not happen.

Basically, I'd like to assign to a variable reference a new value, without creating a new reference. But a function argument a is distinct from the variable v it represents. The object pointed at are the same, but the adress of the two variables are different. Hence, assigning a new value to a cannot change the value of v in any way.

In other words, every variable has its own address, and there's not any other way to change what it points at, than referencing it explictly.

And in my case, because ref is "private" to the function, there's no way to access it from somewhere else.

Too bad...

3
  • Am I wrong or "ref=ref.element;" in your 1st example should read "ref=obj.element;" ? Commented Oct 2, 2009 at 20:29
  • 1
    And shouldn't bar(obj) in that same place be bar(ref)? Commented Oct 2, 2009 at 21:00
  • right, I hesitated between both. Fixed. Commented Oct 3, 2009 at 0:04

1 Answer 1

1

Since there is no byRef in JavaScript, your code and the following are the same

function bar(obj) {
    return {'an':'object'};
}

function foo() {
    var obj = {};
    obj.element = bar();
}

Yet, you may do something similar to this:

function Foo() {        
    this.that = this,
    this.obj = {};
    this.bar = function () {
        var newObject = {'an':'object'};
        that.obj['element'] = newObject;
    }

    return this;
}

var f = Foo();

f.obj['foo'] = 'BAR';
f.bar();
console.log(f.obj.element.an);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering, but I don't get it. In my example, obj ends up being equal to {'an':'object'}, while in yours, obj, at the ends of foo, is equal to {'element':{'an':'object'}}, which therefore is not equivalent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.