Given that I am using John Resig's class (found here: Class), is there a way for a javascript object to proxy it's variables to another object?
Example:
var Car = Class.extend({
init: function(mileage, color, type) {
this.mileage = mileage;
this.color = color;
this.type = carDatabase[type];
}
));
// This would be loaded in as a datasource, not sitting in global
// space like this.
var carDatabase = {
"Falcon": {
"year": 2013,
"engine": "Inline 8",
"company": "Ford"
},
"Commodore": {
"year": 2012,
"engine": "V8",
"company": "Holden"
},
// etc etc
};
// Using the Car class somewhere:
var myCar = new Car(10000, "blue", "Falcon");
console.log(myCar.color); // blue
console.log(myCar.type.year); // 2013
console.log(myCar.type.company); // Ford
So given the example above, can I proxy forward the type into the Car class itself without duplicating the contents of the type.
Ideally I would rather type myCar.company, instead of myCar.type.company for consistency of the Class.
I know that underscore and jQuery both offer extend methods, but they seem to duplicate the content into the original object. I have also considered the fly weight pattern (of which I would argue is overkill, and I would come to the same sticking point as above).
var car = myCar.type;then use car.year ...