How to effectively debug method chained function arguments?

风流意气都作罢 提交于 2019-12-06 00:06:14

问题


Have a look at the following code structure:

myFunction(
  _(myArray)
    .filter({ keep: true })
    .uniq('id')
    .value()
);

myFunction() takes as its argument the result of some array transformation using lodash. While I like the style of the code, I find it hard to debug and end up refactoring it to have the lodash code inside another function or assign it to a variable first, then pass the variable to myFunction().

Do you know of an effective way to debug the function argument code without refactoring? Some thoughts:

  • As is, one cannot add console.log's right in place.
  • In Chrome DevTools it's not possible to set a breakpoint e.g. between .filter() and .uniq()

回答1:


The best way is insert step:

.tap(console.log)

Other ways:

create mixin function

var debug = function (val) {
    console.log(val);
    return val;
}
_.mixin({'debug': debug})

and use it like

_(myArray)
    .debug()
    .filter({ keep: true })
    .debug()
    .uniq('id')
    .debug()
    .value()

or you can override lodash functions before it use like

_.filter = _.wrap(_.filter, function(func, val) {
    console.log(func(val));
    return func(val);
});


来源:https://stackoverflow.com/questions/40608241/how-to-effectively-debug-method-chained-function-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!