3

For example, I have a example function, it has two parameters a and b.

var example = function(a, b){...}

Now I want to wrap this function, do some changes to parameter b, how can I get a in my wrap function?

example = _.wrap(example, function(original)){
    //do some changes to b
    ....
    original(?, b)//how can i get a
}

1 Answer 1

4

You will get the original parameter as arguments to the wrapper function in the same order as it was passed after the original function argument.

In the below example a and b will be the 2 parameters passed to the hello function.

var hello = function(p1, p2) {
  return p1 + ": " + p2;
};
hello = _.wrap(hello, function(func, a, b) {
  return "before, " + func(a, b + 'Name') + ", after";
});
snippet.log(hello('hellow', 'User'));
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>

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

Comments

Your Answer

Draft saved
Draft discarded

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.