1

So I'm reading through a book and it uses this method to overload functions-

function addMethod(object,name,fn){
  var old = object[name];
  object[name] = function(){
    if (fn.length == arguments.length){
      return fn.apply(this,arguments);
    } else if(typeof old == 'function'){
        return old.apply(this,arguments);
    }
  }
}

I have a few questions on this.

  1. Why is fn.length still in scope when the function that's passed in is called? Shouldn't executing the addMethod have caused fn to go out of scope?
  2. Why isn't the arguments referring to the arguments property of the anonymous function, rather than the arguments property of the fn function? (which it should I thought?)
1
  • Notice that it will overload only if the original function (if there) has a different number of arguments. Quite obvious since javascript is untyped, but worth mentioning since some other (typed) language allows overloading with same argument count . A consequence being that you cannot 'overload' a function that does not require all its arguments to be used. (x = x || 0 ; ...) Commented Feb 7, 2014 at 1:06

1 Answer 1

2
  1. The parameter "fn" is in scope because that's how JavaScript works. It's part of the closure around that anonymous function.
  2. The anonymous function created is the replacement for the original function bound to the object with the property name "name". When called, it checks the arguments actually passed on that particular call by looking at the arguments object. If it sees that the number of arguments passed is the same as the number of formal parameters in the "fn" function, then it calls that function. Otherwise, it calls the previous ("old") function, if it exists and is a function.
    A key thing to understand is that the .length property of a function instance gives you the number of formal parameters in the declaration. For example, the .length for that "addMethod" function would be 3.

The way that closures work in JavaScript took me a while to really "get" because I was a C programmer for a long time. In languages like that, space for the function call local variables (etc) is allocated on the stack, and when the function exits it's all popped off the stack. JavaScript just doesn't work that way.

Sign up to request clarification or add additional context in comments.

2 Comments

so the fn still exists at time of calling that function because there exists a reference to fn from within the anonymous function's closure?
Yes. The parameter (essentially, a local variable) from the actual function call to "addMethod" will "stick around" essentially forever; as long as that object property (the replaced function) is still there. So every call to the new replacement function will be able to access it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.