Javascript:如何在Raphael路径上调用对象方法?

Javascript:如何在Raphael路径上调用对象方法?,javascript,raphael,Javascript,Raphael,我有一个JavaScript对象,它包含许多Raphael路径。(路径组成了一堆饼图,它们都在同一张纸上绘制。)我希望单击每个路径时触发一个对象方法。然而,当我点击一个路径时,我会得到“UncaughtTypeError:undefined不是一个函数”(Chrome for Mac OS)。有人知道怎么做吗?以下是我代码的精髓: // Definition of PieChartStack object function PieChartStack() { this.setNodeT

我有一个JavaScript对象,它包含许多Raphael路径。(路径组成了一堆饼图,它们都在同一张纸上绘制。)我希望单击每个路径时触发一个对象方法。然而,当我点击一个路径时,我会得到“UncaughtTypeError:undefined不是一个函数”(Chrome for Mac OS)。有人知道怎么做吗?以下是我代码的精髓:

// Definition of PieChartStack object
function PieChartStack() {

    this.setNodeTree = function (nodeTree) {
        this.nodeTree = nodeTree;
        ...
        this.performInitialSetup();
    }

    this.performInitialSetup = function() {
        ...
        var paper = Raphael("holder", "100%", "100%");
        ...
        paper.customAttributes.segment = function (x, y, r, a1, a2) {
            ...
            return {
                path: [["M", x, y], ["l", r * Math.cos(a1), r * Math.sin(a1)], ["A", r, r, 0, +flag, 1, x + r * Math.cos(a2), y + r * Math.sin(a2)], ["z"]],
                fill: "hsb(" + clr + ", .75, .8)"
            };
        };

        this.handleSliceTap = function(chartIndex, sliceIndex) {
            console.log(chartIndex + " , " + sliceIndex);
        }

        for (var chartIndex = 0; chartIndex < this.pieCharts.length; chartIndex++) {
            ...
            for (sliceIndex = 0; sliceIndex < sliceCount; sliceIndex++) {
                ...
                var path = paper.path().attr({segment: [this.centerX, this.centerY, 1, start, start + val], stroke: "#fff"});

                // PROBLEM HERE ======================================
                path.click(
                    function (e) {
                        this.handleSliceTap(chartIndex, sliceIndex);
                    }
                );
                //====================================================
            }
        }
    }
    return this;
}
//PieChartStack对象的定义
函数PieChartStack(){
this.setNodeTree=函数(nodeTree){
this.nodeTree=nodeTree;
...
this.performInitialSetup();
}
this.performInitialSetup=函数(){
...
var paper=Raphael(“持有人”、“100%”和“100%”);
...
paper.customAttributes.segment=函数(x、y、r、a1、a2){
...
返回{
路径:[“M”,x,y],“l”,r*Math.cos(a1),r*Math.sin(a1)],[“A”,r,r,0,+flag,1,x+r*Math.cos(a2),y+r*Math.sin(a2)],[“z”],
填充:“hsb(“+clr+”,.75,.8)”
};
};
this.handleSliceTap=函数(chartIndex、sliceIndex){
log(chartIndex+,“+sliceIndex);
}
对于(var chartIndex=0;chartIndex
Teemu明白了--“this”在这个上下文中是路径,而不是PieChartStack。通过创建一个新的var:var self=this——然后像这样做来修复:

self.handleSliceTap(chartIndex, sliceIndex);

单击处理程序中的此
不是您所认为的
console.log(此)
在该函数中为您清除内容。当调用
单击
处理程序时,
图表索引
切片索引
都将超出范围。我假设错误发生在
handleSliceTap()
中,当它尝试对这些索引执行任何操作时。Teemu,你确定了它--“this”在这个上下文中是路径,而不是PieChartStack。谢谢