1

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);
2
  • Definitely there's an error in console... Commented Apr 24, 2014 at 17:35
  • 1
    Wow, it was the type. l to L fixed it. Cheers everyone. Commented Apr 24, 2014 at 17:45

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

aren't we craving for case-insensitive identifiers :)
@xtofl - nope we are not, case sensitivity suits me just fine !
1

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/

3 Comments

I was surprised to see Wind.Data..... But actually it's safer than this.Data.... in @adeno's answer.
@xtofl - It's not "safer", it's actually somewhat bad form to reference the literal directly by name inside the literal. When using this as a reference one has to be aware of the context the function is called in, and what value this actually has, but in most cases it will be the object literal unless specifically called with a different value for this (apply, call etc).
In my opinion it is better to use "this" keyword, when you are following the prototype pattern. In all other cases I would avoid using "this".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.