How to call the Calling Function on a Function.Prototype

How to call the Calling Function on a Function.Prototype



When creating a Function.prototype how do you extract the function that is calling the prototype method without having the specify the function names? I have been researching and found that Javascript doesn't have super functionality, however all of the replacement methods that I have found seem to require using specific method names. Super with Known Prototype However, I want to be able to call the super of the Function.prototype without the prototype being on a specific constructor function.




Function.prototype.wrap = (func) =>
return (...args)=>
return func(/*Function Caller*/, ...args);



function testFunc(arg)
console.log(arg);


testFunc = testFunc.wrap((originalFunction, args)
console.log("Testing Wrap ---");
originalFunction(args);
;



How can a pull the function that is calling the Function.prototype.wrap method and inject it into the secondary function without specifying the function names.




1 Answer
1



Arrow functions are lexical scoped, meaning that when writing an arrow function as a prototype method the this is inherited from the current scope, making it bind to the window object. This prevented the this keyword from binding to the function that called .wrap, and meant the code didn't work as expected.



Solution


Function.prototype.wrap = function (func)
return (...args)=>
return func(this, ...args);




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)