简体   繁体   English

如何与.on同时调用console.log和console.log()?

[英]How can I invoke console.log and console.log() simultaneously with .on?

I want to be able to format the console.log() of an event returned, but I can't figure out how to invoke it console.log and console.log() together. 我希望能够格式化返回事件的console.log(),但是我不知道如何一起调用console.log和console.log()。

.on("transactionHash", console.log)

This works normally, and returns a transaction hash with no formatting. 这可以正常工作,并返回没有格式的事务哈希。

.on("transactionHash", function(){console.log('*** Tx Hash:', console.log, '***') })

This prints the strings, but doesn't print the hash. 这将打印字符串,但不打印哈希。 If you use console.log() without declaring a function, it is an error. 如果使用console.log()而不声明函数,则错误。 How can I use both of these with .on simultaneously? 如何在.on同时使用这两个.on

Make sure the callback accepts arguments, and apply console.log with the arguments. 确保回调接受参数,然后将console.log与参数一起应用。 It also doesn't make sense to log the console.log function itself, you probably want to leave that out: 登录console.log函数本身也是没有意义的,您可能想忽略掉它:

.on("transactionHash", function(...args){
  console.log('*** Tx Hash:');
  console.log.apply(console, args);
});

If you know that there's only going to be one argument (or a particular number of arguments), then: 如果您知道只有一个参数(或特定数量的参数),则:

.on("transactionHash", function(arg){
  console.log('*** Tx Hash:');
  console.log(arg);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM