问题
I'd like to get Intellisense support in Visual Studio for the options objects I use in my method calls.
It's common to configure a call to a function in Javascript with a catch-all options object - for example jquery's Ajax call uses:
$.ajax(settings);
Where settings is just an object like:
$.ajax({
url: '/blah',
data: { stuff: 1 },
method: 'POST',
// etc
});
Although it's an implicit object, the properties follow a specific class. Typically when you have something like this that's important for Intellisense/describing code but not for the code to work you put this in a -vsdoc.js file. But how do I get Intellisense to come up for this object?
I looked at jquery-vsdoc.js for an example, since it's provided by Microsoft - to no avail. In one case they just type it as "Object" and the other they just don't document it at all.
I've tried this for example - in fillTable.js:
function fillTable(options) {
/// <param name="options" type="FillTableOptions">Options to fill table</param>
And in fillTable-vsdoc.js:
function FillTableOptions() {
/// <field type="String">Id property</field>
this.idProp = 'Id';
But all I get for Intellisense is that the type is FillTableOptions - when I then create the object I get no Intellisense help while picking properties.
So, how do I get Intellisense support for the properties of an object like this?
回答1:
I'm not aware of any way to get true Intellisense support on object parameters outside of TypeScript... however, you can format the parameter documentation nodes to some extent.
For example, you can enter whatever text you like into the 'type' attribute, so long as it is a continuous block of text (doesn't contain any spaces or breaking characters).
Ex. 1: custom type annotations.
function where(collection, evaluator) {
/// <summary>
/// Equivalent to a WHERE clause.
/// </summary>
/// <param name="collection" type="array||object">Accepts an array or associative array (object).</param>
/// <param name="evaluator" type="function(key,value,index)">Applied to each element of the @collection to determine which elements to return.</param>
/// <returns type="array||object" />
}
Ex. 2: multi-line parameter documentation (
is a new line).
function modal(options) {
/// <summary>
/// Displays a modal window.
/// </summary>
/// <param name="options" type="object||string">
/// If a string is passed in, will be treated as @heading.
///
/// * heading {string} The heading text.
/// ? content {string} The text content; overridden by @htmlContent.
/// ? htmlContent {string} Use when HTML content is desired; overrides @content.
/// ? settings {object} FancyBox.js settings.
/// </param>
}
This won't give you true Intellisense for object parameters in the way that TypeScript will, but it will give you more control over the styling of the comments and should help reduce the ambiguity.
来源:https://stackoverflow.com/questions/15047178/visual-studio-javascript-intellisense-options-object