最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript pass by object by reference then update - Stack Overflow

programmeradmin2浏览0评论

I've been searching all over SO and I know there are a lot of topics about this but I haven't found one that answered my question.

I saw a question about getting an object value back from a string like this:

function getPropertyByString(str) {
    var properties = str.split(".");
    var myTempObject = window[properties[0]];
    for (var i = 1, length = properties.length; i < length; i++) {
        myTempObject = myTempObject[properties[i]];
    }
    return myTempObject;
}

So if there is a global variable called myGlobalVar, you could pass the string 'myGlobalVar.someProp.stateName' and assumming that is all valid you would get back the value of stateName say Arizona for example.

How could I update that property to California now?

If I try

var x = getPropertyByString('myGlobalVar.someProp.stateName');
x = 'California';

that will update the value of x and not the object.

I tried

var x = getPropertyByString('myGlobalVar.someProp.stateName');
x.value = 'California';

that didn't work either.

Can someone please help me to understand this with my example?

Thanks

I've been searching all over SO and I know there are a lot of topics about this but I haven't found one that answered my question.

I saw a question about getting an object value back from a string like this:

function getPropertyByString(str) {
    var properties = str.split(".");
    var myTempObject = window[properties[0]];
    for (var i = 1, length = properties.length; i < length; i++) {
        myTempObject = myTempObject[properties[i]];
    }
    return myTempObject;
}

So if there is a global variable called myGlobalVar, you could pass the string 'myGlobalVar.someProp.stateName' and assumming that is all valid you would get back the value of stateName say Arizona for example.

How could I update that property to California now?

If I try

var x = getPropertyByString('myGlobalVar.someProp.stateName');
x = 'California';

that will update the value of x and not the object.

I tried

var x = getPropertyByString('myGlobalVar.someProp.stateName');
x.value = 'California';

that didn't work either.

Can someone please help me to understand this with my example?

Thanks

Share Improve this question asked Dec 22, 2011 at 16:14 blong824blong824 4,03015 gold badges57 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try the following;

function setPropertyByString(path, value) {
    var steps = path.split("."),
        obj = window,
        i = 0,
        cur;

    for (; i < steps.length - 1; i++) {
        cur = obj[steps[i]];

        if (cur !== undefined) {
            obj = cur;
        } else {
            break;
        };
    };

    obj[steps[i]] = value;
}

It'd work by using it such as;

setPropertyByString('myGlobalVar.someProp.stateName', 'California');

You can see it in action here; http://jsfiddle/xCK8J/

The reason yours didn't work is because strings are immutable in JavaScript. You are re-assigning the variable x with the value 'California', rather than updating the location it points to to be 'California'.

If you'd have done;

var x = getPropertyByString('myGlobalVar.someProp');
x.stateName = 'California';

You'd see it works; as you're manipulating the object pointed to by x, rather than reassigning x to be something else. The above is what the setPropertyByString() method does behind the scenes; it just hides it from you.

This would do the trick:

myGlobalVar.someProp.stateName = "California"

So would this:

myGlobalVar["someProp"].stateName = "California"

or this:

myGlobalVar["someProp"]["stateName"] = "California"

Alternatively,

var x = getPropertyByString('myGlobalVar.someProp');
x.stateName = "California"

Note that if, in my last example, I do something like this:

x = {stateName:"California"};

It will not change the value of myGlobalVar.someProp.stateName.

Using = assigns a new value to the variable on the LHS. This is not the same thing as assigning a new value to the referent of the variable.

发布评论

评论列表(0)

  1. 暂无评论