我再次偶然发现了我不理解的 Javascript 行为。一旦对象外部的变量发生更改,我就需要更新对象的属性。外部变量在对象属性中被引用,所以我认为我所要做的就是从外部更改变量并自动更改属性值。
这是代码外观的简化版本:
var serverPath = "123/";
var GetCurrentProductionApiConfig = {
URL: {
GetStart: serverPath + 'GetCurrentProduction?returnValue=start&',
GetEnd: serverPath + 'GetCurrentProduction?returnValue=end&',
Get: serverPath + 'GetCurrentProduction?returnValue=start&'
}
};
serverPath = "456/";
console.log(GetCurrentProductionApiConfig.URL.GetStart);
这将导致:
123/GetCurrentProduction?returnValue=start&
是因为变量已被复制(通过值传递)而不是在其上有一个指针(通过引用传递)?哪种方式是更新属性的正确方式?
至尊宝的传说
相关分类