Underscore.JS - tap 方法

语法

_.tap(object, interceptor)

tap 方法将拦截器应用于对象,然后返回该对象。请参阅以下示例 −

示例

var _ = require('underscore');

_.chain([1, 2, 3, 4, 5, 6])
  .filter(function(num) { return num % 2 == 0; })
  .tap(console.log)
  .map(function(num) { return num * num })
  .value();

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

{ 2, 4, 6 }

underscorejs_updating_objects.html