开发者

How do I combine 2 prewritten Javascript functions

Would I want to nest one fun开发者_StackOverflowction into another?


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


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

Or prototype's wrap function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜