当我写代码时,让发生的事情变得明显对我来说非常重要,这样我(或其他人)就不需要浪费时间来重新弄清楚一切。因此,我首先寻求有关回调命名约定的最佳实践。
此外,最近从PHP(Symfony2)切换到在IDE中使用DocBlock进行提示,我正试图找到一种类似的方法来在Javascript代码中获得有用的提示。
例如:
# app.js
# .. requires etc
// Question 1. Naming the function (`function importFinishedHandler (..`) makes it easier to debug (see callbackhell.com). But do I really need to assign this to a variable, as it seems to work without, is there any difference?
var importFinishedHandler = function importFinishedHandler (err, data) {
# handle err or data
};
var importer = new Importer();
importer.import('../path/to/myfile.json', importFinishedHandler);
# importer.js
// Question 2. Is it good practice to name the `callback` here something descriptive such as `importFinishedHandler` (instead of just `callback` which is what I see a lot of people doing), as passed in from the `app.js` file.
// Question 3. When I want to see what parameters the `importFinishedHandler` callback function requires, I need to go back into `app.js` and check manually. In this instance it is pretty easy to see, but when files get big it isn't. Is there any way to use `Docblock` or something so that my IDE (WebStorm) can give me the correct required parameters?
Importer.prototype.import = function (file, importFinishedHandler) {
# do import stuff
importFinishedHandler(null, data);
};
如果有人能回答这些问题,或将我与一些好的文件联系起来并提出建议,我将不胜感激。到目前为止,我发现了一个有用的:
http://callbackhell.com/
但是像docblock callbacks
这样的搜索查询并没有产生任何有用的结果。
这可能有助于JavaScript中的docblockhttps://devdocs.magento.com/guides/v2.4/coding-standards/docblock-standard-javascript.html
说到回调命名约定的最佳实践我认为使用"关于";前缀是一个好的前缀,例如onSuccess
、onFailure
。看看这个reddit评论