【发布时间】:2015-03-15 18:11:55
【问题描述】:
Chrome Extension Notification API 有一个带有以下签名的 create 方法:
notifications.create(string notificationId, object options, function callback)
我实际上没有需要在通知完成时运行的回调,但如果我省略回调,它会引发错误:
响应 storage.get 时出错:错误:调用表单 notification.create(string, object, null) 与定义不匹配
很公平。我不会写很多 JavaScript。当我需要 noop 回调时,使用此 API 的最佳方式是什么?
【问题讨论】:
-
传递一个空函数?
notifications.create("idHere", {options:here}, function() {}); -
优秀。这就是我的想法,但我想我最好确保这不是一种更惯用的方法。
-
如果 API 无论如何都没有提供在不传递回调的情况下做同样的事情,那么你就会被回调。没什么大不了的。如果它是一个你不想打印的字符串,你会传递“”。在面向对象的世界中,您将使用空对象模式并传递一个在调用其方法时什么都不做的对象。
-
Iirc,使用其他一些 chrome api,您可以省略第三个参数:
notifications.create ("idHere", object)。 -
注意:有一个 Chrome 错误,但它没有更新多年:code.google.com/p/chromium/issues/detail?id=163750
标签: javascript google-chrome-extension noop