简体   繁体   中英

javascript, saving reference to nested object literal

Lets say I have the following object:

name = {
    name_one : {
        name_one_A : {
            name_one_A_a : 'John',
            name_one_A_b : 'Kate'
        }
    }
};

I could create a reference to 'John' by doing:

current_name = name.name_one.name_one_A.name_one_A_a;

Lets say I'm referencing "name.name_one.name_one_A" several times, is there a way to create a referencing to this nesting? This doesn't work, but something like:

A = name.name_one.name_one_A;
name = A.name_one_A_b;

'name' would then equal 'Kate'. I know this doesn't work like that, but I'm just wondering if there is a way to accomplish this?

Thanks for any help!

It's a bit hard to tell exactly what you're asking which has caused some confusion among the answers.

If you're referencing name.name_one.name_one_A multiple times, you can save that once and then use it:

var x = name.name_one.name_one_A;
x.name_one_A_a = 'Bill';
x.name_one_A_b = 'Sue';

This works ONLY because the value of name.name_one.name_one_A is an object (Object, Array or Function). So, when you save it to another variable, you aren't actually saving a reference to name.name_one.name_one_A , but rather getting the value of that property which is itself an object. And, when you then modify that object, since name.name_one.name_one_A also points to that same object, you will see the value change there too.


Javascript does not have the ability to create a reference to a particular property on an object such that you could use only that reference to then change the value of that property.

Using C/C++ terminology, you can't create a pointer to a property on an object in Javascript such that you could change the value in that property using only that pointer.

You would instead have to pass the host object and the property name and you could then change the value of the property on that host object.


Or, if the value of the property was itself an object (Object, Array or Function), then you can get the value of the property and then change the object that it points to.


So, in your data structure:

var name = {
    name_one : {
        name_one_A : {
            name_one_A_a : 'John',
            name_one_A_b : 'Kate'
        }
    }
};

There's no way to get a direct reference to:

name.name_one.name_one_A.name_one_A_a

that would let you modify just the contents of that property at some later time. Instead, you'd have do something like this where you get a reference to the containing object and use that:

var obj = name.name_one.name_one_A;
var prop = "name_one_A_a";

// and then some time later:
obj[prop] = 'Bob';
// or
obj.name_one_A_a = 'Bob';

Firefox Scratchpad had an issue with a variable named "name", but this works:

var foo = {
'name_one' : {
    'name_one_A' : {
        'name_one_A_a' : 'John',
        'name_one_A_b' : 'Kate'
    }
}
};

var A = foo.name_one.name_one_A;
console.log(A.name_one_A_b);

//yields

Kate

Update:

You can get a reference that is able to change a property value:

var foo = {
    'name_one' : {
        'name_one_A' : {
            'name_one_A_a' : 'John',
            'name_one_A_b' : 'Kate'
        }
    }
};

var A = foo.name_one.name_one_A;
console.log(A.name_one_A_b);
A.name_one_A_b = "bob";
console.log(A.name_one_A_b);
console.log(JSON.stringify(foo));

Yields:

"Kate"
"bob"
"{"name_one":{"name_one_A":{"name_one_A_a":"John","name_one_A_b":"bob"}}}" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM