Closed altocumulus closed 5 years ago
The reference commit would be https://github.com/d3/d3-zoom/commit/a116a77ebd496495e25070bee6294824a5e2feaf
@Fil Any idea on why that was introduced? The commit message just reads "Update README."...
My guess is that some scales' invert function might need to access this, so this would be a proper generic way of calling them.
The answer is: it’s good practice in JavaScript. If the object’s methods happen to be bound to the object, it’s unnecessary to specify this, but one generally shouldn’t assume so unless it is documented (and not likely to change in a future version).
This is inspired by the interesting question Map function in D3, confused about multiple functions passed in which was posted on StackOverflow more than 2 years and ago and is still unresolved. That question asks why at some locations involving d3-zoom a scale object is passed as the
this
context to a scale's.invert()
method like in this Block.As
scale.invert()
does not make any use ofthis
the call will work just as well if the second argument is omitted. This looked like a small glitch in just one demo at first, interestingly though, it also shows up in the d3-zoom module's own source code, namely, in the methodstransform.rescaleX()
andtransform.rescaleY()
. Those functions' bodies are like this:Right in the last call while passing
x.invert
as the callback to the.map()
method, thex
—the scale instance—is passed as the second argument whereby setting thethis
context. Again, this is not needed nor is it a real error.Long story short: Is there a deeper reason for explicitly passing in the scale as the
this
context to the.invert()
method or can we get rid of it for the sake of simplicity and clarity?