문제

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