函数访问函数中自己的对象属性

Function to access own object properties in function

本文关键字:函数 属性 对象 访问 自己的      更新时间:2023-09-26

如何从getValuePerTick()访问maxValuenumberOfTicks?尝试我的第一个JavaScript程序时遇到了一些问题。谢谢

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);
                    }
                }
            };
         // ...
    }
});

由于maxValuenumberOfTicks是对象的属性,因此需要使用成员表示法来访问它

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

仅使用this.前缀

return (this.maxValue / this.numberOfTicks);