object encapsulation system javascript
//sample int object for testing
var IntObj = function(val) {
this.val = val;
this.type = "int";
};
var ObjCapsule = function(obj, valname) {
this.obj = obj;
this.valname = valname;
this.val = obj[valname];
ObjCapsule.prototype.set = function(val) {
this.obj[this.valname] = val;
this.val = this.obj[this.valname];
};
ObjCapsule.prototype.get = function() {
return this.val;
};
};
/* var a = new IntObj(6);
var b = ObjCapsule(a, "val");
b
var b = new ObjCapsule(a, "val");
b
=> { obj: { val: 6, type: 'int' }, valname: 'val', val: 6 }
b.set(5)
a
=> { val: 5, type: 'int' }
b
=> { obj: { val: 5, type: 'int' }, valname: 'val', val: 5 }
*/