简体   繁体   English

Javascript属性作为对其他对象属性的引用

[英]Javascript property as reference to other object's property

Is there a way to assign properties of one object as references to the properties of another, and do so dynamically? 有没有一种方法可以将一个对象的属性分配为对另一个对象的属性的引用,并且可以动态地进行分配? Note that in the for loop, I've skipped any property that has the same name as the second object. 请注意,在for循环中,我跳过了与第二个对象同名的所有属性。 I'm working on a framework that will cache JSON as objects with behaviors and allow ORM kind of behavior, where I can grab cached objects and collections as properties of other cached objects. 我正在开发一个框架,该框架会将JSON缓存为具有行为的对象,并允许进行ORM行为,在这里我可以将缓存的对象和集合作为其他缓存对象的属性来获取。 I need to skip certain properties to avoid circular reference. 我需要跳过某些属性以避免循环引用。

var obj1 = {
    prop1: "hey",
    obj2:"you",
    prop2: "come over here"
}

var obj2 = {}

for(var prop in obj1){
    if(prop != 'obj2'){
        obj2[prop] = obj1[prop];
    }
}
console.log(obj1);
console.log(obj2);

obj1.prop2 = "come on, man";

console.log(obj1);
console.log(obj2);
//obj1 is unchanged in output.  I would like to be able to update it by mutating obj2's properties

fiddle: http://jsfiddle.net/6ncasLb0/1/ 小提琴: http : //jsfiddle.net/6ncasLb0/1/

If this is not possible, is it possible to remove or mutate a property of a reference without mutating the original object? 如果无法做到这一点,是否可以在不更改原始对象的情况下删除或更改引用的属性? I know, probably not. 我知道,也许不是。 Just a shot in the dark. 在黑暗中只是一枪。

I guess the closest you can get to it, is to make sure the property you are changing it the same property you are getting on both objects, so you would need to do some work to make sure they "know" each other when they are instantiated (eg, clone from the original object) 我想您可以找到的最接近的方法是,确保要更改的属性与在两个对象上获得的属性相同,因此,您需要做一些工作以确保它们在彼此“认识”时实例化(例如,从原始对象克隆)

As an example, you could use a simplified model like this, any properties marked in its creation would also update the original object, though new properties defined on the object should be fine). 例如,您可以使用这样的简化模型,尽管在对象上定义的新属性应该很好,但在其创建中标记的任何属性也会更新原始对象。 Note that enumrating and just referencing the properties wouldn't work, at least not with strings (objects would change when copied from 1 object to another) 请注意,枚举和仅引用属性是行不通的,至少不能用于字符串(当从一个对象复制到另一个对象时,对象会发生变化)

 ; (function(namespace) { function addProperty(obj, property, valueholder) { Object.defineProperty(obj, property, { get: function() { return valueholder[property]; }, set: function(val) { valueholder[property] = val; }, enumerable: true, configurable: false }); } var model = namespace.model || function(options) { if (typeof options === 'undefined') { options = {}; } var propHolder = options.container || {}, prop; if (typeof options.props != null) { for (prop in options.props) { if (options.props.hasOwnProperty(prop)) { addProperty(this, prop, propHolder); propHolder[prop] = options.props[prop]; } } }; namespace.model.prototype.clone = function() { var options = { props: {}, container: propHolder }, prop; for (prop in this) { if (this.hasOwnProperty(prop)) { options.props[prop] = this[prop]; } } return new namespace.model(options); }; namespace.model.prototype.toString = function() { var prop, msg, props = []; for (prop in propHolder) { if (propHolder.hasOwnProperty(prop)) { props.push(prop + ': "' + this[prop].toString() + '"'); } } return '[Model] {' + props.join(', ') + '}'; } return this; }; namespace.model = model; }(window)); var obj1 = new model({ props: { prop2: "come over here" } }); obj1.prop1 = 'Hey'; obj1.obj2 = 'You'; obj1.test = { a: 10 }; var obj2 = obj1.clone(); console.log('-- before changes --'); console.log(obj1.toString()); console.log(obj2.toString()); obj2.prop2 = "come on, man"; obj2.prop1 = "won't change"; obj2.obj2 = "also not"; obj2.test.b = "both have this now"; console.log('-- after changes --'); console.log(obj1.toString()); console.log(obj2.toString()); console.log(obj1.test); console.log(obj2.test); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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