What's causing your issue is the use of an arrow function.
In arrow functions, the this
keyword may reference to:
- window (if accessed globally, not in strict mode),
- undefined (if accessed globally, in strict mode),
- an instance (if in a constructor),
- an object (if in a method or function inside an object or instance) or
- a binded/applied value.
To fix your issue, you have to use a "normal" function()
and you can call your function using call(this, ...params)
:
- In
this
you put the object you want the keyword this
to be bound to and
- In
..args
you put your function's parameters if any.
So, with the above in mind, your code can be as shown:
Array.prototype.myfunc = function(...) {
// Your code
}
var array = [1, 69, -1, 1];
var result = myfunc.call(array, ...);
- Using
array
as the first argument in call()
, you bind the this
keyword to array
.