Javascript 设置对对象属性的引用
我有一个对象数组,我想设置并重置对其中一个对象属性的引用。假设我有以下几点:Javascript 设置对对象属性的引用,javascript,Javascript,我有一个对象数组,我想设置并重置对其中一个对象属性的引用。假设我有以下几点: var TheReference = null; var TheArrayOfObjects = []; var TheObject = {}; TheObject.SomeProp1 = "test1"; TheObject.SomeProp2 = "test2"; TheArrayOfObjects.push(TheObject); // this array could contain hundreds of
var TheReference = null;
var TheArrayOfObjects = [];
var TheObject = {};
TheObject.SomeProp1 = "test1";
TheObject.SomeProp2 = "test2";
TheArrayOfObjects.push(TheObject); // this array could contain hundreds of objects
TheReference = TheObject.SomeProp1; // here I know it's not a reference.
TheReference = "update"; // so of course it doesn't update the object's property
我的目标是存储对对象属性的引用,然后通过访问该引用来更新该属性。如果我有
TheReference=TheObject
,那么这将允许我访问该特定对象,但我希望访问该对象的属性,以便我可以写入TheReference=“update”
,并且该值在对象的属性中更新。存储对对象属性的引用的方法是什么?如果对象属性本身是“真”对象,则只能存储对该对象属性的引用。上面的代码无法按要求工作,因为您试图引用字符串,但这将:
var TheReference = null;
var TheArrayOfObjects = [];
var TheObject = {};
TheObject.SomeProp1 = {p1: "test1", p2: "test2"};
TheObject.SomeProp2 = {p3: "test3", p4: "test4"};
TheArrayOfObjects.push(TheObject); // this array could contain hundreds of objects
TheReference = TheObject.SomeProp1; // here I know it's not a reference.
alert(TheReference.p1); // will show 'test1'
如果对象的属性本身是“true”对象,则只能存储对该属性的引用。上面的代码无法按要求工作,因为您试图引用字符串,但这将:
var TheReference = null;
var TheArrayOfObjects = [];
var TheObject = {};
TheObject.SomeProp1 = {p1: "test1", p2: "test2"};
TheObject.SomeProp2 = {p3: "test3", p4: "test4"};
TheArrayOfObjects.push(TheObject); // this array could contain hundreds of objects
TheReference = TheObject.SomeProp1; // here I know it's not a reference.
alert(TheReference.p1); // will show 'test1'
我会质疑javascript中的动机,但也许您想隐藏原始对象并只公开它的一个属性。您可以创建一个闭包来更新对象的属性,但不公开原始对象:
function update(obj, prop, val) {
if(!val) return obj[prop];
obj[prop] = val;
}
var theRef = null,
theArr = [],
theObj = {
one: 'test one',
two: 'test two'
},
refToPropOne;
theArr.push(theObj);
refToPropOne = update.bind(null, theObj, 'one');
console.log(theArr); // [{one: "test one", two: "test two"}]
refToPropOne('new val');
console.log(theArr); // [{one: "new val", two: "test two"}]
refToPropOne('replaced');
console.log(theArr); // [{one: "replaced", two: "test two"}]
console.log(refToPropOne()); // "replaced"
我会质疑javascript中的动机,但也许您想隐藏原始对象并只公开它的一个属性。您可以创建一个闭包来更新对象的属性,但不公开原始对象:
function update(obj, prop, val) {
if(!val) return obj[prop];
obj[prop] = val;
}
var theRef = null,
theArr = [],
theObj = {
one: 'test one',
two: 'test two'
},
refToPropOne;
theArr.push(theObj);
refToPropOne = update.bind(null, theObj, 'one');
console.log(theArr); // [{one: "test one", two: "test two"}]
refToPropOne('new val');
console.log(theArr); // [{one: "new val", two: "test two"}]
refToPropOne('replaced');
console.log(theArr); // [{one: "replaced", two: "test two"}]
console.log(refToPropOne()); // "replaced"
在js中你不能这样做,但可能你想在js中你不能这样做,但可能你想在alert do
TheReference={some new obje}
和TheObject之后做类似的事情。SomeProp1
也更改为{some new obje}
@grundy,这毫无意义。这违背了计算机科学的基本“指针”逻辑。为什么不呢?例如,在c#中,可以使用out
或ref
参数修改器执行此操作。此外,在某些编程语言中,您可以向变量添加别名,或者在C语言中,您可以获取成员变量的地址并将其保存在指针中。但是,OP want after alert doTheReference={some new obje}
和对象。SomeProp1
也更改为{some new obje}
@grundy,这毫无意义。这违背了计算机科学的基本“指针”逻辑。为什么不呢?例如,在c#中,可以使用out
或ref
参数修改器执行此操作。此外,在某些编程语言中,您可以将别名添加到variable;在C语言中,您可以获取成员变量的地址并将其保存在指针中。