user1316642
user1316642

Reputation: 71

Function to access own object properties in function

From getValuePerTick() how do I access maxValue and numberOfTicks? Trying my hand at my first JavaScript program and having some trouble. Thanks!

var TChart = Object.extend(
{
    init: function(canvasName)
    {
       // ...
       this.config = {
                gutter: {
                    top: 25,
                    right: 100,
                    bottom: 50,
                    left: 0
                },
                scale: {
                    maxValue: 3.3,
                    numberOfTicks: 10,
                    getValuePerTick: function() {
                        return (maxValue / numberOfTicks);
                    }
                }
            };
         // ...
    }
});

Upvotes: 0

Views: 47

Answers (2)

Balint Bako
Balint Bako

Reputation: 2560

Simply with this. prefix

return (this.maxValue / this.numberOfTicks);

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Since maxValue and numberOfTicks are properties of the object, you need to use member notation to access it

   this.config = {
            gutter: {
                top: 25,
                right: 100,
                bottom: 50,
                left: 0
            },
            scale: {
                maxValue: 3.3,
                numberOfTicks: 10,
                getValuePerTick: function() {
                    return (this.maxValue / this.numberOfTicks);
                }
            }
        };

Upvotes: 4

Related Questions