Pho3nixHun
Pho3nixHun

Reputation: 828

Javascript object in an other object

I searching for an hour now (w/o any success), that how can I define an object in an other object (in javascript):

function UserStat(arr) {
    var arrx = arr;
    this.day = function(dateofday) {
        //Some code going here which results will be stored variables like:
        this.a = someInnerFunction();
        this.b = someOtherFunction();
    }
}

I'd like to access these variable when I create an instance of the outer function, somehow like this if this is possible:

var value = new UserStat(arr1).day('2012-10-20').a

Thank you in advance for any help!

Upvotes: 1

Views: 114

Answers (2)

kabaros
kabaros

Reputation: 5173

function UserStat(arr) {
    var arrx = arr;
    this.day = function(dateofday) {
        //Some code going here which results will be stored variables like:
        var dayFunc = {
            a: someInnerFunction,
            b: otherFunc           
        }
        return dayFunc;
    }
}
​

Upvotes: 0

jonvuri
jonvuri

Reputation: 5940

I'm not sure how you wanted to use the dateofday variable, but this would work:

function UserStat(arr) {
    var arrx = arr;
    this.day = {
        a: someInnerFunction,
        b: someOtherFunction
    };
}

new UserStat().day.a();

So would this:

function UserStat(arr) {
    var arrx = arr;
    this.day = (function(date){
        var obj = {};
        obj.a = someInnerFunction;
        obj.b = someOtherFunction;
        return obj;
    }(dateofday));
}

Or even this:

function UserStat(arr) {
    var arrx = arr;
    this.day = new function() {
        this.a = someInnerFunction,
        this.b = someOtherFunction
    };
}

Upvotes: 3

Related Questions