Essentially I want to wrap (not extend) a function but the wrapper shall be callable just like the function. I can use a function (Example 1 or 2) to execute the call
function.
My question is why can't I just copy the call
function (Example 3)? If I do this, I get the error Function.prototype.call called on incompatible Object
function FunctionWrapper( description, functionReference ) {
this.description = description;
/* 1 */ this.call = function( thisArg ) { functionReference.call( thisArg ) };
/* 2 */ this.call = thisArg => functionReference.call( thisArg );
/* 3 */ this.call = functionReference.call;
}
function StringHolder( string ) {
this.string = string;
}
StringHolder.prototype.log = function() {
console.log( this.string );
};
let logger = new FunctionWrapper( "This is a logger", StringHolder.prototype.log );
logger.call( new StringHolder( "bar" ) );