【问题标题】:Method of object as handler对象作为处理程序的方法
【发布时间】:2014-10-24 15:12:22
【问题描述】:

我试试这个:

function MovableLine (line, number) {
    this.line = line;
    this.number = number;
}

MovableLine.prototype.start = function (e) {

    alert(this.number);
    alert(this);
};

然后:

var mline = this.xAxis[0].plotLinesAndBands[plotLinesCount].svgElem;
                mline.css({
                    'cursor': 'pointer'
                });
                mline.translate(0, 0);
                movableLine = new MovableLine(mline, 10);
                movableLine.line.on('mousedown', movableLines[plotLinesCount].start);

结果:
第一个警报:未定义
第二个警报:对象 SWGPathElement

如何从 start() 获取我的对象可移动线?

【问题讨论】:

  • 您在哪里创建和添加到可移动线?

标签: jquery highcharts jquery-on


【解决方案1】:

我建议不要将原型用于事件方法。将函数分配为对象属性,并缓存this 引用:

function MovableLine (line, number) {
    var self = this; //Caching
    this.line = line;
    this.number = number;
    this.start = function (e) {

        alert(self.number);
        alert(self);
    };
}

如果您绝对想使用.prototype,也可以使用点击捕捉器:

function MovableLine (line, number) {
    var self = this; //Caching
    this.line = line;
    this.number = number;
    this.clickCatcher = function () {
        self.start.apply(self, Array.prototype.slice.call(arguments, 0));
    };
}

MovableLine.prototype.start = function (e) {

    alert(this.number);
    alert(this);
};

//And you event binding:
movableLine.line.on('mousedown', movableLines[plotLinesCount].clickCatcher);

【讨论】:

    猜你喜欢
    • 2013-06-26
    • 2017-12-28
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多