I have a prototype
function written on the Array
class like
Array.prototype.myfunc = () =>
{
// ...
}
and within the body this
is referring to window
when I call it on an array like
var result = [1, 69, -1, 1].myfunc();
How can I make it refer to the array on which it's being invoked?
The () => { }
arrow syntax binds to the current this
which likely is window
(depending on when you assign the function).
The fat arrow syntax in ES6 is an implicit bind to the current scope.
If your current scope is Window
(or the global scope), then the function you just set on the prototype is bound to that scope.
You still need the good ol' function ()
syntax for behavior without binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With