Q:

javascript object pattern for a periodically updating object

I want to periodically update a js object (after an initialization when the object is created), and was attempting the following:
var data = {
    foo: -1,
    bar: -1,
    refresh: function() {
        this.foo = getFoo();
        this.bar = getBar();
    }()
}

This doesnt currently work at the moment, the this doesnt seem to refer to the data object.  There are a few different permutations I could try here - but wondering if i am barking up the wrong tree here?  What sort of pattern should I be using for an object that I need to periodically update (called externally). 

A:

You're pretty close, ditch those ending ():
var data = {
    foo: -1,
    bar: -1,
    refresh: function() {
        this.foo = getFoo();
        this.bar = getBar();
    } // <== here
};    // <== Also, semicolon here for best practice

Usage:
data.refresh();

You'll need to provide getFoo and getBar functions, or accept arguments into refresh, e.g.:
var data = {
    foo: -1,
    bar: -1,
    refresh: function(newFoo, newBar) {
        this.foo = newFoo;
        this.bar = newBar;
    }
};

Usage:
data.refresh(42, 52);

