I have 2 Javascript objects, Limits and Wind, and are trying to call the function on the one object that is a parameter of the other object. The alert is never called and there's no errors in the console. Am I doing this right?

var Limits = {
    minx: 0,
    miny: 0,

    SetMaxLimits: function (x1, x2, y1, y2, zoom) {
        alert('j');
    }
};


var Wind = {
    Data    : Limits,
    Screen  : Limits,
    Rotation: 0,
    CanvasW : 0,
    CanvasH : 0,
    North   : true,
    _Math   : true,

    NormalLimits: function (x,y) {
        Data.SetMaxlimits(0, 0, 0, 0, 0);
    }
};

Wind.NormalLimits(0,0);
有帮助吗?

解决方案

You have a typo, it's SetMaxLimits, not SetMaxlimits, and you have to reference the object that the Data method is a property of, which in this case could probably be done with this, depending on how the function is called.

var Limits = {
    minx: 0,
    miny: 0,

    SetMaxLimits: function (x1, x2, y1, y2, zoom) {
        alert('j');
    }
};


var Wind = {
    Data    : Limits,
    Screen  : Limits,
    Rotation: 0,
    CanvasW : 0,
    CanvasH : 0,
    North   : true,
    _Math   : true,

    NormalLimits: function () {
        this.Data.SetMaxLimits(0, 0, 0, 0, 0);
    }
};

Wind.NormalLimits();

FIDDLE

其他提示

It is scoping problem. You are trying to access Data, which is not accessible in the context of the NoarmaLimits function. If you access it through Wind object though it will work.

var Wind = {
    Data    : Limits,
    Screen  : Limits,
    Rotation: 0,
    CanvasW : 0,
    CanvasH : 0,
    North   : true,
    _Math   : true,

    NormalLimits: function () {
        Wind.Data.SetMaxLimits(0, 0, 0, 0, 0);
    }
};

http://jsfiddle.net/PYTcR/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top