Borrowing Array method to an object with call


This Content is from Stack Overflow. Question asked by Gio

I just come across the concept of decorator and apply-call methods.
Why should I use an apply method here, even if it works without that. I can just pass “return func(…args)” and it still works. I understood the concept if I was working with an object method while having a returning this I should have used “apply” or “call”,but why is it forwarding the call here with a function? On top of that, how the “.calls” property is getting passed to work.calls ?

    function work(a, b) {
      alert( a + b ); 

function spy(func) {

  function wrapper(...args) {
    return func.apply(this, args);

  wrapper.calls = [];

  return wrapper;
    work = spy(work);
    work(1, 2); 
    work(4, 5); 
    for (let args of work.calls) {
      alert( 'call:' + args.join() ); 


Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?