javascript - 定义函数返回类型,以便在编写函数调用时显示在工具提示中
问题描述
我希望能够设置函数返回类型,以便我的 IDE 可以应用 Intellisense。
例如,我有一个函数,它接受一个对象并从中创建/返回一个 HTMLElement。当我像这样调用那个函数时,我想要那个:
var myElement = document.createElementWithProperties({elementType: 'div', id: 'myElement',style:{background: 'blue'}});
当我输入“myElement”时,在下一行。例如,它会建议我 appendChild。
解决方案
您可以使用JSDoc文档块来装饰您的函数。例如这样的事情:
/**
* Creates an element with the specified properties.
*
* @param {object} The properties to create the element with.
* @return {HTMLElement} The HTML element.
*/
function createElementWithProperties(obj) {
// Do stuff!
}
注意:在 Visual Studio Code 中,当扩展本机文档对象时,这似乎不起作用,但作为独立函数或用户定义对象的一部分应该可以正常工作。
推荐阅读
- ms-access - 使用 Ms Access 的条码
- python - 我的 Python 代码有多少个线程?
- django - 我们如何使用表格中的 django orm 使用单独列表中的值添加额外的字段?
- javascript - 我无法在事件处理程序中执行道具父方法
- tensorflow - 如何在 Tensorflow 中嵌入离散 ID?
- r - 如何创建随机附加行并将其附加到现有数据框中?
- html - 仅使用 CSS,显示 HTML5 元素内文本的最后 3 个字母
- python - 方法显示为数据属性
- testng - 在 TestNG 中断言测试用例的正确方法是什么?
- angular6 - 如何在 angular4 或更高版本中更改 mouseover 和 mouseleave 上的图像 src?