ryandlf
ryandlf

Reputation: 28545

Calling a function from an object property without using brackets

Although my title seems kinda wonky, I tried to describe exactly what I am trying to do. Say I have an object:

function testObj(data) {
    this.id = data.id
    this.getObjFromId = function() {
        //Return some value from somewhere using the this.id
    }
    this.obj = this.getObjFromId();
}

The reason for doing this is so I can update the this.id property and this.obj will always return some other object using the updated id. Right now what this does is it initially calls the getObjFromId function using the original id and stores the returned value in this.obj. If I do something like this:

var testObj = new TestObj({id: 1});
alert(testObj.obj); //Returns Foo1
testObj.id = 5; //Update the object with a new id
alert(testObj.obj); //Returns Foo1, but should return Foo5

This would be simple if I could just call the this.obj function as a function like this.obj(), but for a variety of reasons this is not possible (templating etc etc.) so I need to be able to get a dynamic value from the this.obj "function" by simple using this.obj.

I hope this makes sense. As i'm writing I understand its confusing, so maybe there is a better approach to doing this. None the less, the functionality I am proposing needs to work the same where I can call a dynamic value from an object property. My questions title might be better suited as something more descriptive of my problem if someone cares to update, but I couldn't think of a better way to describe.

Upvotes: 1

Views: 234

Answers (2)

Why don't you call getObjFromId directly like

var testObj = new TestObj({id: 1});
alert(testObj.getObjFromId());

Additionally, your code is not optimal since it will create an instance of getObjFromId for every TestObj instance you create. You should declare getObjFromId as a value of TestObj's prototype:

function TestObj(data) {...}
TestObj.prototype.getObjFromId = function(){...}

EDIT

If calling getObjFromId directly is not an option, then I would create another function to update object's state. Instead of calling

testObj.id = 'newId';

you would call

testObj.update('newId');//internally, call getObjFromId and/or update id field

Upvotes: 0

Bergi
Bergi

Reputation: 664297

You want to use a getter function for your obj property:

function TestObj(data) {
    this.id = data.id
    Object.defineProperty(this, "obj", {
        get: function() {
            //Return some value from somewhere using the this.id
        }
    });
}

as well, you could use a setter function for the id property which updates the obj property only when you set the id. This could be useful for caching.

function TestObj(data) {
    var id = data.id, obj;
    Object.defineProperty(this, "id", {
        get: function() { return id; },
        set: function(newval) {
            id = newval;
            obj = this.getObjFromId();
    });
    Object.defineProperty(this, "obj", {
        get: function() { return obj; }
    });
}
TestObj.prototype.getObjFromId = function() {
    //Return some value from somewhere using the this.id
};

Upvotes: 2

Related Questions