1

我现在搜索了一个小时(没有任何成功),我如何在另一个对象中定义一个对象(在 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();
    }
}

当我创建外部函数的实例时,我想访问这些变量,如果可能的话,就像这样:

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

预先感谢您的任何帮助!

4

2 回答 2

3

我不确定您想如何使用 dateofday 变量,但这会起作用:

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

new UserStat().day.a();

也可以这样:

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

甚至这样:

function UserStat(arr) {
    var arrx = arr;
    this.day = new function() {
        this.a = someInnerFunction,
        this.b = someOtherFunction
    };
}
于 2012-10-25T21:51:01.660 回答
0
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;
    }
}
​
于 2012-10-25T21:53:26.273 回答