I am wondering, is it possible to emulate the Function.prototype.call
method?
I've implemented an emulation of bind()
, the Douglas Crockford example is well known, by using apply()
in a wrapper function.
Can I do the same for call()
, without using apply
, or bind
?
It's not something I would use of course in the real world, but just out of academic interest!
So far I havn't been able to figure this one, except using implicit binding - which has obvious drawbacks
'use strict';
// obj to bind as this, params is an array of parameters
function customCall( obj, params ){
var funToCall = this;
obj.funToCall = funToCall; // Implicit binding
obj.funToCall(...params); // Obviously not a good idea
}
var objA = {
a: "objA value"
};
function test(num1, num2){
debugger;
console.log(this.a);
console.log(num1, num2);
}
test.customCall = customCall;
test.customCall(objA, [1,2]) //objA value, 1 2