开发者

How do I combine 2 prewritten Javascript functions

开发者 https://www.devze.com 2022-12-27 14:56 出处:网络
Would I want to nest one fun开发者_StackOverflowction into another?Is this what you\'re asking? function add1(x){

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

精彩评论

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

关注公众号