下划线包装功能有什么作用? [关闭]

问题描述 投票:1回答:1

看一下这里给出的例子(http://underscorejs.org/#wrap),我真的不明白包装在做什么......更重要的是,当函数被“包装”时,感觉必须设置参数(例如,做什么打招呼('john')?是否有其他可用的例子解释了包装的全部内容?它的典型用例是什么?谢谢!C

node.js underscore.js
1个回答
1
投票

_.wrap()可以在回调函数中接受除您所具有的函数之外的更多参数。例如,为了让你的hello('John')示例工作,我们需要对示例代码进行一些修改

var hello = function(name) { return "hello: " + name; };
hello = _.wrap(hello, function(func,name) {
  return "before, " + func(name) + ", after";
});
hello('John');
© www.soinside.com 2019 - 2024. All rights reserved.