views:

72

answers:

2

Would I want to nest one function into another?

A: 

Is this what you're asking?

function add1(x){
  return x + 1;
}

function mult2(x){
    return x * 2;
}

function add1ThenMult2(x){
  return mult2(add1(x));
}

add1ThenMult2(10); // --> 22

or, hiding the 2 functions ...

  var add1ThenMult2 = (function(){

    function add1(x){
      return x + 1;
    }

    function mult2(x){
      return x * 2;
    }

    function add1ThenMult2(x){
      return mult2(add1(x));
    }

    return add1ThenMult2;

  }());

  add1ThenMult2(10); // --> 22
z5h
A: 

You might consider jquery-aop for adding advice around or before a given function.

Or prototype's wrap function.

wombleton