首页 > 解决方案 > 定义函数返回类型,以便在编写函数调用时显示在工具提示中

问题描述

我希望能够设置函数返回类型,以便我的 IDE 可以应用 Intellisense。

例如,我有一个函数,它接受一个对象并从中创建/返回一个 HTMLElement。当我像这样调用那个函数时,我想要那个:

var myElement = document.createElementWithProperties({elementType: 'div', id: 'myElement',style:{background: 'blue'}});

当我输入“myElement”时,在下一行。例如,它会建议我 appendChild。

document.createElement 示例

标签: javascriptfunctionreturn-type

解决方案


您可以使用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 中,当扩展本机文档对象时,这似乎不起作用,但作为独立函数或用户定义对象的一部分应该可以正常工作。


推荐阅读