1

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).

3
  • @dc5's answer is correct, but it will probably perform worse than copying the data, and have less browser support. Commented Aug 18, 2013 at 4:50
  • why not just use a buffer? var car = myCar.type; then use car.year ... Commented Aug 18, 2013 at 7:26
  • @technosaurus Because car would then miss things such as color/mileage which are particular to that one instance. The other elements are shared (such as engine and company) but elements/properties such as mileage only pertain to a particular car Commented Aug 18, 2013 at 7:52

2 Answers 2

2

You can use defineProperty which has support for defining get/set methods for a property among other things.

The MDN article referenced also has a compatibility table, but it is generally supported in recent versions of all browser with some limitations.

Since you mentioned John Resig, he has a good blog post "ECMAScript 5 Objects and Properties" that is a bit older, but still a good read. It was written in May, 2009 and he states early on in the post that some of the examples and specs may change.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes. Using ES6 Proxy() you can create traps for property get and set events.

const handler = {
    get(object, property) { 
        if(object.hasOwnProperty(property)){
            for(var prop in object[property]){
                this[prop] = object[property][prop] //set class instance props
            }               
        }
        return object[property]; // don't need to return
    }
};


var carDatabaseProxy = new Proxy(carDatabase, handler)

class Car {
    constructor(mileage,color,type){
         this.mileage = mileage;
         this.color = color;
         carDatabaseProxy[type]; // just need to get 
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.