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.
精彩评论