Skip to content

Commit

Permalink
instead of .bind(this) just save const that = this; and use that
Browse files Browse the repository at this point in the history
  • Loading branch information
mirabilos committed Feb 9, 2023
1 parent 90c8f45 commit 2121206
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/dygraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,9 +1114,10 @@ Dygraph.prototype.createRollInterface_ = function() {
roller.value = this.rollPeriod_;
utils.update(roller.style, textAttr);

const that = this;
roller.onchange = function onchange() {
return this.adjustRoll(roller.value);
}.bind(this);
return that.adjustRoll(roller.value);
};
};

/**
Expand Down Expand Up @@ -1331,11 +1332,12 @@ Dygraph.prototype.doZoomXDates_ = function(minDate, maxDate) {
var old_window = this.xAxisRange();
var new_window = [minDate, maxDate];
const zoomCallback = this.getFunctionOption('zoomCallback');
const that = this;
this.doAnimatedZoom(old_window, new_window, null, null, function animatedZoomCallback() {
if (zoomCallback) {
zoomCallback.call(this, minDate, maxDate, this.yAxisRanges());
zoomCallback.call(that, minDate, maxDate, that.yAxisRanges());
}
}.bind(this));
});
};

/**
Expand All @@ -1361,12 +1363,13 @@ Dygraph.prototype.doZoomY_ = function(lowY, highY) {
}

const zoomCallback = this.getFunctionOption('zoomCallback');
const that = this;
this.doAnimatedZoom(null, null, oldValueRanges, newValueRanges, function animatedZoomCallback() {
if (zoomCallback) {
const [minX, maxX] = this.xAxisRange();
zoomCallback.call(this, minX, maxX, this.yAxisRanges());
const [minX, maxX] = that.xAxisRange();
zoomCallback.call(that, minX, maxX, that.yAxisRanges());
}
}.bind(this));
});
};

/**
Expand Down Expand Up @@ -1425,16 +1428,17 @@ Dygraph.prototype.resetZoom = function() {
newValueRanges = this.yAxisExtremes();
}

const that = this;
this.doAnimatedZoom(oldWindow, newWindow, oldValueRanges, newValueRanges,
function animatedZoomCallback() {
this.dateWindow_ = null;
this.axes_.forEach(axis => {
that.dateWindow_ = null;
that.axes_.forEach(axis => {
if (axis.valueRange) delete axis.valueRange;
});
if (zoomCallback) {
zoomCallback.call(this, minDate, maxDate, this.yAxisRanges());
zoomCallback.call(that, minDate, maxDate, that.yAxisRanges());
}
}.bind(this));
});
};

/**
Expand Down Expand Up @@ -1470,18 +1474,19 @@ Dygraph.prototype.doAnimatedZoom = function(oldXRange, newXRange, oldYRanges, ne
}
}

const that = this;
utils.repeatAndCleanup(function (step) {
if (valueRanges.length) {
for (var i = 0; i < this.axes_.length; i++) {
for (var i = 0; i < that.axes_.length; i++) {
var w = valueRanges[step][i];
this.axes_[i].valueRange = [w[0], w[1]];
that.axes_[i].valueRange = [w[0], w[1]];
}
}
if (windows.length) {
this.dateWindow_ = windows[step];
that.dateWindow_ = windows[step];
}
this.drawGraph_();
}.bind(this), steps, Dygraph.ANIMATION_DURATION / steps, callback);
that.drawGraph_();
}, steps, Dygraph.ANIMATION_DURATION / steps, callback);
};

/**
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ axes.prototype.willDrawChart = function(e) {
};
};

const that = this;

if (g.getOptionForAxis('drawAxis', 'y') || g.getOptionForAxis('drawAxis', 'y2')) {
if (layout.yticks && layout.yticks.length > 0) {
var num_axes = g.numAxes();
Expand All @@ -183,7 +185,7 @@ axes.prototype.willDrawChart = function(e) {
/* Tick marks are currently clipped, so don't bother drawing them.
context.beginPath();
context.moveTo(halfUp(x), halfDown(y));
context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
context.lineTo(halfUp(x - sgn * that.attr_('axisTickSize')), halfDown(y));
context.closePath();
context.stroke();
*/
Expand Down Expand Up @@ -211,8 +213,8 @@ axes.prototype.willDrawChart = function(e) {
}
label.style.width = getAxisOption('axisLabelWidth') + 'px';
containerDiv.appendChild(label);
this.ylabels_.push(label);
}.bind(this));
that.ylabels_.push(label);
});
}

// draw a vertical line on the left to separate the chart from the labels.
Expand Down Expand Up @@ -257,7 +259,7 @@ axes.prototype.willDrawChart = function(e) {
/* Tick marks are currently clipped, so don't bother drawing them.
context.beginPath();
context.moveTo(halfUp(x), halfDown(y));
context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
context.lineTo(halfUp(x), halfDown(y + that.attr_('axisTickSize')));
context.closePath();
context.stroke();
*/
Expand All @@ -279,8 +281,8 @@ axes.prototype.willDrawChart = function(e) {
label.style.left = left + 'px';
label.style.width = getAxisOption('axisLabelWidth') + 'px';
containerDiv.appendChild(label);
this.xlabels_.push(label);
}.bind(this));
that.xlabels_.push(label);
});
}

context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
Expand Down

3 comments on commit 2121206

@mirabilos
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danvk I think this change should be safe (and help performance), but I’d appreciate if you (or someone else who actually knows JS ☺) could look it over

It goes on top of a17153e (which was necessary because PhantomJS triggered one of the _newArrowChecks Babel inserted in spec mode so I had to disable the latter and make the remaining arrow functions that do use this safe manually).

@danvk
Copy link
Owner

@danvk danvk commented on 2121206 Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://twitter.com/bendhalpern/status/578925947245633536? :)

"This" (your commit) looks fine. If you have arrow functions available, that's a preferable way to preserve the this-binding from the outer scope.

@mirabilos
Copy link
Collaborator Author

@mirabilos mirabilos commented on 2121206 Feb 9, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.